Up until this point, the saving and loading has been working fine. However, with the newest value (3), it suddenly wont save because the ‘value cannot be converted to a number.’ All of the other values are saving fine, and 3 is an int value with the same parameters as all of the other values. What might be causing this error?
local dss = game:GetService("DataStoreService")
local playerdata = dss:GetDataStore("123Save123")
---------- Player Statistics ----------
local One = game.ReplicatedStorage.One
local Two = game.ReplicatedStorage.Two
local Three = game.ReplicatedStorage.Three
game.Players.PlayerAdded:Connect (function(player)
player.CharacterAdded:Connect (function(character)
------- Load Data ---------------------------------
local data = playerdata:GetAsync(player.UserId)
if data ~= nil then
One.Value = data["One"]
Two.Value = data["Two"]
Three.Value = data["Three"]
else
local success,err = pcall(function()
playerdata:SetAsync(player.UserId,startData)
end)
if success then
else
end
end
------- Save Game ----------------------------------------
game.OnClose = function()
local saveData = {
["One"] = One.Value,
["Two"] = Two.Value,
["Three"] = Three.Value
}
local success,err = pcall(function()
playerdata:UpdateAsync(player.UserId, saveData)
end)
end
end)
end)
When printed, the value is always “0” just like the others, so I am not positive what is setting it apart.
To add on to what @Nitefal said, if you do use a number make sure to use [1] so that it treats it like a value, I’m not 100% sure what [] does but I know that it will make sure the script treats it like a string and not a number. I do not believe it works with variables though.
local one = game.ReplicatedStorage[1]
local two = game.ReplicatedStorage[2]
local three = game.ReplicatedStorage[3]
@SlayterZ, I’m not sure if this will help, but if the “One, Two, and Three” Values are only being accessed by the Server, they do not need to be in ReplicatedStorage. Also:
local success,err = pcall(function()
playerdata:SetAsync(player.UserId,startData) -- does startData exist???
end)
I do not see “startData” defined earlier in the script, am I missing something or is it not there?
Not technically a bug, but a result of their update. It’s the player ID messing it up.
This is what I got to work:
local dss = game:GetService("DataStoreService")
local playerdata = dss:GetDataStore("123Save123")
---------- Player Statistics ----------
local One = game.ReplicatedStorage.One
local Two = game.ReplicatedStorage.Two
local Three = game.ReplicatedStorage.Three
game.Players.PlayerAdded:Connect (function(player)
player.CharacterAdded:Connect (function(character)
------- Load Data ---------------------------------
local data = playerdata:GetAsync(tostring(player.UserId))
if data ~= nil then
One.Value = data["One"]
Two.Value = data["Two"]
Three.Value = data["Three"]
else
end
------- Save Game ----------------------------------------
game:BindToClose(function()
local saveData = {
["One"] = One.Value,
["Two"] = Two.Value,
["Three"] = Three.Value
}
local success, err = pcall(function()
playerdata:SetAsync(tostring(player.UserId), saveData)
end)
if not success then
warn(err)
end
end)
end)
end)