I have this function when saving data, but for some reason player isn’t an index
local function saveData(player)
local playerUserId = "Player_"..player.UserId
local data = {
Drinks = player.leaderstats.Drinks.Value;
}
local success, errormessage = pcall(function()
myDataStore:SetAsync(playerUserId, data)
end)
if success then
print("Data succefully saved!")
else
print("Data saving error")
warn(errormessage)
end
end
The error in the title happens when you call local playerUserId = "Player_"..player.UserId but player is nil. You should check if player exists before continuing on with the function. Something to note, however, is that if player is nil it might mean you have an error somewhere else in your code.
You might not be “sending” the player object when the players leaves.
You should do
game.Players.PlayerRemoving:connect(saveData) right?
Are you doing that, and are you ONLY calling this when the player is leaving?
Another thing, is that, do you have a wait before actually calling this function?
Again, what could be happening, and is probably the issue is that, PLAYER got removed before the function could load. So now the PLAYER variable is NIL. since it’s not there anymore. And it seems like the problem is not on this part of the script, but somewhere else, if you agree, you should be sending the part of the script where you call this function.