Why is my DataStore inconsistent?

Hey so i’m using a data store right now. Loading the values works perfectly but it does not always save when I leave. Sometimes it saves sometimes it doesn’t. Usually it saves but sometimes it doesn’t. When it doesn’t save there is no warnings printed and it just doesn’t say “Successfully saved data”. Why would this code be inconsistent? How can I make it more consistent?

local function SaveData(player)
	if data[player.UserId] then --- data is a table which stores all the usersIDs in it with all their stats among it.
		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 did not save retrying")
				task.wait()
			end

		until success or attempt == 6

		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")
	end
	
end
1 Like

Can you also send line that is calling this function?

1 Like
Players.PlayerRemoving:Connect(function(player)
	SaveData(player)
	data[player.UserId] = nil
end)

I think i just found out why… I should add a task.wait before setting their data to nil

1 Like

Why u set data to nill anyways?

1 Like

because otherwise when server stays open for ages the table will become huge

1 Like

That’s not the problem here. SaveData(player) already yields the script so it can’t run data[player.UserId] = nil before saving

Instead, put this at the end of the code

game:BindToClose(function()
	if RunService:IsStudio() then
		task.wait(3)
	end
end)
1 Like

If its only working in studio then it will work fine in game?

Problem is the issue doesn’t just happen in studio. it also happens in game I tested both

I think it should be

game:BindToClose(function()
	task.wait(3)
end)
1 Like

I also have this for when server closes

game:BindToClose(function()
	if not RunService:IsStudio() then 
		for index, player in pairs(Players:GetPlayers()) do 
			task.spawn(function()
				SaveData()
			end)
		end
	end
end)
game:BindToClose(function()
	if not RunService:IsStudio() then 
		for index, player in pairs(Players:GetPlayers()) do 
			task.spawn(function()
				SaveData()
			end)
		end
	end
    task.wait(3)
end)

Just in case put wait there

Thats the problem right there, the function SaveData() is getting called twice. First, when PlayerRemoving gets fired, it saves the actual data and sets the data[player.UserId] to nil. And then SaveData() gets called again in BindToClose. But the second time, it saves nil since you set the data[player.UserId] to nil the first time.

Put this code instead

game:BindToClose(function()
	if RunService:IsStudio() then
		task.wait(2)
	end
end)

It isn’t getting called twice in game it’s only doing that when the server shuts down and player isn’t removing. Or am I wrong for this and

the wait seems to have fixed it, it saved 10/10 times

Are you saying that you also tested it in Roblox Player?

game will not shut down until function in game:BindToClose() is done

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