I am trying to make an Obby game but I keep on getting this error.
03:27:04.602 value of type nil cannot be converted to a number - Server - Leaderstats:26
03:27:04.602 Stack Begin - Studio
03:27:04.602 Script 'ServerScriptService.Leaderstats', Line 26 - Studio - Leaderstats:26
03:27:04.602 Stack End - Studio
I dont know why it keeps on doing this but here is the code:
local DatastoreService = game:GetService("DataStoreService")
local plrdata = DatastoreService:GetDataStore("plrdata")
local checkpointsfolder = game.Workspace.Ceckpoints
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder", plr)
leaderstats.Name = "leaderstats"
local Stage = Instance.new("IntValue", leaderstats)
Stage.Name = "Stage"
Stage.Value = 1
local Deaths = Instance.new("IntValue", leaderstats)
Deaths.Name = "Deaths"
Deaths.Value = 0
local data
local userid = plr.UserId
local success, ermsg = pcall(function()
data = plrdata:GetAsync(userid)
end)
if success then
if data then
Stage.Value = data.Stage
Deaths.Value = data.Deaths
end
else
print("Could Not Find Data")
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local data = {Stage = plr.leaderstats.Stage.Value ;
Deaths = plr.leaderstats.Deaths.Value ;
}
local userid = plr.UserId
local success, ermsg = pcall(function()
plrdata:SetAsync(userid, data)
end)
if success then
print("Data Saved")
else
print("Data not Saved")
end
end)
Okay so the data is a table and in the table the Deaths value is nil.
Have you tried to leave the game and join again ?
Or also try this, I don’t think that will change anything but try :
local DatastoreService = game:GetService("DataStoreService")
local plrdata = DatastoreService:GetDataStore("plrdata")
local checkpointsfolder = game.Workspace.Ceckpoints
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder", plr)
leaderstats.Name = "leaderstats"
local Stage = Instance.new("IntValue", leaderstats)
Stage.Name = "Stage"
Stage.Value = 1
local Deaths = Instance.new("IntValue", leaderstats)
Deaths.Name = "Deaths"
Deaths.Value = 0
local data
local userid = plr.UserId
local success, ermsg = pcall(function()
data = plrdata:GetAsync(userid)
end)
if success then
if data then
Stage.Value = data.Stage
Deaths.Value = data.Deaths
end
else
print("Could Not Find Data")
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local data = {
["Stage"] = plr.leaderstats.Stage.Value,
["Deaths"] = plr.leaderstats.Deaths.Value,
}
local userid = plr.UserId
local success, ermsg = pcall(function()
plrdata:SetAsync(userid, data)
end)
if success then
print("Data Saved")
else
print("Data not Saved")
end
end)
To add data checking, when you load the data, you could just do:
if data == nil then
data.Stage = 1
data.Deaths = 0
end
And a BindToClose function:
--you need to convert your saving function to this format:
local function save(player)
--saving code goes in here
end
game:BindToClose(function()
if game["Run Service"]:IsStudio() then task.wait(3) return nil end
for _, player in ipairs(game:GetService("Players"):GetPlayers()) do
save(player)
end
task.wait(3)
end)