Data is nil in studio but not ingame?

So pretty self explanatory, if I join in Studio, my data returns nil but if I join in the game, it’s there. Also, some people are reporting that they lost all their data and it’s just all placeholder values which is what it’s like for me in Studio. This is the code:

local data = {}

local function loadData(player)

	local success = nil
	local playerData = nil
	local attempt = 1

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

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

	if success then
		if not playerData then--give default data if they're new
			playerData = {
				["Gems"] = 0,
				["SelectedTowers"] = {"Cameraguy"},
				["OwnedTowers"] = {"Cameraguy"},
				["MaxTowers"] = 5,
				["RedeemedCodes"] = {}
			}
			beamEvent:FireClient(player)
		end
		
		data[player.UserId] = playerData
		
		if player.UserId == 1317259399 or player.UserId == 1545290587 or player.UserId == 1524202189 or player.UserId == 339310190 or player.UserId == 2747319006 or player.UserId == 2375714402 or player.UserId == 470119913 then
			playerData = {
				["Gems"] = 9999999,
				["SelectedTowers"] = {"Cameraguy"},
				["OwnedTowers"] = {"Cameraguy","Speakerguy","Cameragal","TV-Guy","Saw Cameraguy","Big Cameraguy","Scientist Cameraguy","Dark Cameragal","TV-Gal","Speakergal","Big TV-Guy","Big Scientist Cameraguy","Big Speakerguy","Dark Speakerguy","Rocket Flying Cameraguy","Supreme Speakerguy","Supreme Cameraguy","Ninja Cameraguy","Supreme Cinemaguy","Upgraded Supreme Cameraguy","Upgraded Supreme Speakerguy","Upgraded Ninja Cameraguy","Upgraded Corrupted Supreme Speakerguy","Corrupted Supreme Speakerguy","Rocket Big Cameraguy","Supreme Pumpkin Sorcerer"},
				["MaxTowers"] = 5,
				["RedeemedCodes"] = {}
			}
			data[player.UserId] = playerData
			
		end
end

--save player data
local function saveData(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:UpdateAsync(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 pairs(Players:GetPlayers()) do
			task.spawn(function()
				saveData(player)
			end)
		end
	else
		print("Shutting down inside studio")
	end
end)

And if I saveData in studio it warns, No session data for my userId. This isn’t too big of a deal but some people are reporting that they just have no data all of a sudden and everything is gone and I think they may be related. Does anyone have any suggestions? (Also the weird thing is I even give myself a set data for testing purposes and it still returns nil in studio)

1 Like

I think you need to enable API access in the game settings.

It is enabled:

Would you think it’s in the saveData function or loadData?

I just did some research, in the saveData function, its success, err not success, playerData.

nvm i just read the code more

Hold on, i think i found something, so I did this:
image
And in studio, it doesn’t print which means loadData isn’t running at all in Studio. But, in game, it prints?
image
I think this is the source of the problem, how come this happens?

This is how I run loadData btw:
image

If there is a PlayerAdded event, it might not be firing because the player connects before the script loads.

How could that be and is there any way to fix it?

You could have a loop that checks for new players that just joined, then load their data. And then add a tag or something to indicate that their data has been loaded.

How could I do that? Could you give sample code?

local cooldown = 0

game:GetService("Run Service").Heartbeat:Connect(function()
    if cooldown < tick() then
        for _, i in game.Players:GetPlayers() do
            if not collectionservice:HasTag(i, "Loaded") then
                loadData(i)
                collectionservice:AddTag(i, "Loaded")
            end
        end

        cooldown = tick() + 1
    end
end)

replace your normal player added function, which may look like this:

game.Players.PlayerAdded:Connect(function(plr)
   print(plr.Name .. " joined!")
end)

with

function PlayerAdded(plr)
   print(plr.Name .. " joined!")
end

for _, Player in ipairs(game.Players:GetPlayers()) do
    PlayerAdded(Player)
end
game.Players.PlayerAdded:Connect(PlayerAdded)

This ensures that every player is processed.

I’m actually having a similar problem to you, I also use a session data system just like yours, hope someone has an idea.

That fixed it in Studio, imma just hope it works in the 50 player servers…

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.