How come playerData is nil?

So for some reason this players data is just nil. Idk why, but its nil. Im confused because I put this:
image
But it just doesn’t work, what do I do?

1 Like

Should I add another nil check or smth?

Why are you using multiple attempts to get the data?

1 Like

Just incase the first one fails

1 Like

Hello, This should be what you are looking for.

local PlayerData = dataBase:GetAsync(player.UserId) or {
    Gems = 0,
    SelectedTowers = {'Cameraguy'},
    OwnedTowers = {'Cameraguy'},
    MaxTowers = 5,
    RedeemedCodes = {},
}

But what would this do though?

Looking at this code snippet, i think it looks ok. It might have to do with the SetAsync, mind showing the whole code?

Shouldn’t this

return dataBase:GetAsync(player.UserId)

be

playerData = dataBase:GetAsync(player.UserId)

I think both is ok, return just sends the data back.

It’s hard to tell if its in a function though. Also, he is checking for the playerData variable later which is still equal to nil.

I know the code, its from a tutorial series (and yes its in a function and playerdata gets assigned in default when there is none)

1 Like

The first part I showed is basically most of it but theres also this saveData function:

--save player data
local function saveData(player)
	print(player)
	if data[player.UserId] and player.UserId ~= 0 then
		local success = nil
		local playerData = nil
		local attempt = 1

		repeat
			success, playerData = pcall(function()
				return dataBase:GetAsync(player.UserId, function()
					return data[player.UserId]
				end)
			end)

			attempt += 1
			if not success then
				warn(playerData)
				task.wait()
			end
		until success or attempt == 3

		if success then
			print("Data saved successfully")
		else
			warn("Unable to save data for player", player.UserId)
		end
	else
		warn("No session data for", player.UserId)
	end
end
Players.PlayerRemoving:Connect(function(player)
	saveData(player)
	data[player.UserId] = nil
end)

game:BindToClose(function()--if game shuts down
	if not RunService:IsStudio() then
		print("Shutting down")
		for index, player in ipairs(Players:GetPlayers()) do
				saveData(player)
		end
	else
		print("Shutting down inside studio")
	end
end)

Did you mean dataBase:UpdateAsync()?

No I meant to use return, that’s how its supposed to be im pretty sure

Maybe try removing that line? Else I am not sure

What does it print? Did you add more debug prints to it?
See if it succeeded, then print something, See if there is an error and then print something etc.
Show us/tell us the outputs

I could be tripping but I don’t think you ever set playerData to the data saved.

This sets the variable ‘PlayerData’ to the pre-existing player data, and if it can’t find pre-existing player data, then it creates data to use.