Roblox 的数据存储服务无法正确同步,但它始终会完美加载。我不明白为什么会发生这种情况,并且它不会保存在游戏中或工作室中。我可靠地保存数据的唯一方法是使用工作室插件“数据存储编辑器”。
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")
local function onPlayerJoin(player)
local PlayerStats = script.PlayerStats:Clone()
PlayerStats.Parent = player
local playerUserId = 'Player_'..player.UserId
local wins = playerData:GetAsync(playerUserId.."-Wins")
local jump = playerData:GetAsync(playerUserId.."-Jump")
print("Player"..player.Name.." has "..wins.." wins and "..jump.." jump power")
if wins then
PlayerStats.Wins.Value = wins
print("Loaded wins for "..playerUserId.." ("..player.Name..")")
else
print("Failed to load wins for "..playerUserId.." ("..player.Name..")")
end
if jump then
PlayerStats.Jump.Value = jump
print("Loaded jump for "..playerUserId.." ("..player.Name..")")
else
print("Failed to load jump for "..playerUserId.." ("..player.Name..")")
end
while wait(10) do
local success, err = pcall(function()
local playerUserId = "Player_"..player.UserId
playerData:SetAsync(playerUserId.."-Wins", player.PlayerStats.Wins.Value)
playerData:SetAsync(playerUserId.."-Jump", player.PlayerStats.Jump.Value)
print("Saved data for "..playerUserId.." ("..player.Name..")")
end)
if not success then
warn("Failed to save data for "..player.UserId.." ("..player.Name..")")
end
end
end
local function onPlayerExit(player)
local success, err = pcall(function()
local playerUserId = "Player_"..player.UserId
playerData:SetAsync(playerUserId.."-Wins", player.PlayerStats.Wins.Value)
playerData:SetAsync(playerUserId.."-Jump", player.PlayerStats.Jump.Value)
print("Saved data for "..playerUserId.." ("..player.Name..")")
end)
if not success then
warn("Failed to save data for "..player.UserId.." ("..player.Name..")")
end
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)
我的 Leaderstats 创建代码可能看起来与平常不同,因为我只是克隆文件夹,其中包含脚本中的所有内容。
我希望它能够正常保存和加载数据,但它只加载,我不明白为什么