Trying to make a variable save after you leave, but it is not working. I also get this error:
value of type nil cannot be converted to a number - Server - Stats:26
Heres my code:
local serverStorage = game:GetService("ServerStorage")
local DataStore = game:GetService("DataStoreService"):GetDataStore("PlayerSave3")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local time = Instance.new("IntValue")
time.Name = "Time"
time.Parent = leaderstats
local timeData
local sucess,errormessage = pcall(function()
timeData = DataStore:GetAsync("time-"..player.UserId)
end)
if sucess then
if time then
time.Value = timeData
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
DataStore:SetAsync("time-"..player.UserId,player.leaderstats.Strength.Value)
end)
end)
Whatever GetAsync is returning, that you are storing in timeData, is nil. The error means its trying to convert nil, which is whats getting returned, to a number, which is what the int value type is. It’s happening when you set the int value’s value to timeData.
Make sure timeData isnt nil. If a player hasnt played the game before, its probably nil, so you need to account for that case if you haven’t already.
This makes sense, but how would I go about doing this? Should I make it set to 0 every time someone joins the game for the first time? Or should I make it wait one second for the int value to no longer be nil?
Also to elaborate on what joeberson said, in your script you make it so if someone has a time datastore then equal it to timeData but if they don’t have that you have no way of giving them a value for time. You would need to add something like.
if sucess then
if time then
time.Value = timeData
else
time.Value = 0
end
end
Without a case for if the player doesn’t have a time datastore it will return nil as the time datastore has no value. Also I just noticed that your datastore variable is PlayerSave3 and it needs to be the same name as the name of the datastore.
I looked into it because I was curious and the only problem is the name of your keys and Datastore.
It should be
local DataStore = game:GetService(“DataStoreService”):GetDataStore(“Time”)
As the name of the leaderstat time.
timeData = DataStore:GetAsync(“Time”…player.UserId)
DataStore:SetAsync(“Time”…player.UserId,player.leaderstats.Strength.Value)
As that too is the name of the Datastore.
I tweaked a few things and this works in the game but not in studio; the error is still there in studio. I think what happened is that when you join the game for the first time, the value is nil. As you play in the game, the value slowly increases causing it to no longer be nil. When you join the game for the first time, the script doesn’t work as there is no saved data. It only when you leave, as now there is saved data if that makes any sense. I just don’t know if I should keep trying to fix the error message when you play the game for the first time.
You should be checking for wether or not timeData is nil not time. because timeData by default for any new player is going to be nil. so doing something like.
if sucess then
if timeData then
time.Value = timeData -- data loaded
else
time.Value = 0 -- data loaded but new player
end
else
-- data didn't load
end
theres no point in doing
if time then
...
end
because time was just initialized as an Instance so it cant be nil