Datastores require a string as a key. You are passing an integer to SetAsync, you will need to convert this to a string using the tostring()
function.
Your corrected code should look like this.
local dataStore = game:GetService("DataStoreService"):GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local silver = Instance.new("IntValue")
silver.Name = "Silver"
silver.Parent = leaderstats
local playerUserId = "Player_"..plr.UserId
local data
local success, errormessage = pcall(function()
data = dataStore:GetAsync(tostring(playerUserId))
end)
if success then
silver.Value = data
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local playerUserId = "Player_"..plr.UserId
local data = plr.leaderstats.Silver.Value
local success, errormessage = pcall(function()
dataStore:SetAsync(tostring(playerUserId), data)
end)
if success then
print("Data successfully saved.")
else
print("Error when saving data.")
warn(errormessage)
end
end)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…