So basically I'm making a roblox clicker simulator game and sometimes when I test the save data for clicks it sometimes gives me this error ServerScriptService.leaderstats:87: attempt to index nil with 'leaderstats'
local success, errormessage = pcall(function()
rebirthsDataStore:SetAsync(playerUserId, rebirthsValue)
end)
-- Saving Gems Data
local gemsValue = player.leaderstats.Gems.Value
local success, errormessage = pcall(function()
gemsDataStore:SetAsync(playerUserId, gemsValue)
end)
end)
game:BindToClose(function(player)
for _, Player in pairs(game.Players:GetPlayers()) do
local playerUserId = "player"..Player.UserId
-- Saving Clicks
local clicksValue = player.leaderstats.Clicks.Value
local success, errormessage = pcall(function()
clicksDataStore:SetAsync(playerUserId, clicksValue)
end)
-- Saving Rebirths
local rebirthsValue = player.leaderstats.Rebirths.Value
local success, errormessage = pcall(function()
rebirthsDataStore:SetAsync(playerUserId, rebirthsValue)
end)
-- Saving Gems Data
local gemsValue = player.leaderstats.Gems.Value
local success, errormessage = pcall(function()
gemsDataStore:SetAsync(playerUserId, gemsValue)
end)
end
end)
Your error is telling you that when you try to access player.leaderstats
, the player
variable doesn't have a value. And this is because the game:BindToClose function does not provide a Player as an argument.
But you are iterating over all of the Players left in the server in a for-loop, just set the name of that iterator variable to be player
and make sure the capitalization is correct in the rest of your code.
game:BindToClose(function()
for _, player in ipairs(game.Players:GetPlayers()) do
local playerUserId = "player" .. player.UserId