DataStore not saving my data in roblox studio, but it DOES work in the roblox game itself

So I was Just messing around trying to make something completely else when I started to notice that My data wasn’t saved anymore in roblox studio. It also didn’t work when I changed my code back to how it was before.

I did some tests with some prints to see what was going wrong, But weirdly enough everything was going as it used to be, the only difference is that when I joined my experience again in roblox studio that my data wasn’t saved. So desperate for a fix I literally reverted my game it’s version back to a version where it was 100% working, but even that didn’t work. Even more confusing is that my data saves perfectly fine when I join my experience from the roblox page itself. So let’s say that I have 10k money, and that get’s saved as usual, but when I join my game again on roblox studio, and I get like 15k cash, then when I rejoin my game in roblox studio it will be set back to 10k.

So here when ever a player already has data it will be added in a table with the player as the key
rb1

And let’s say that there isn’t any data then I call a function that will basically create default values if this is the first time the player joins the game.

And if the player leaves then this function will be called to store his data.
rb3

I Searched for solutions, but I couldn’t find any, and I am really confused on why it does work when I join the game when I join it on the roblox platform page, but that it doesn’t work when I join my game in roblox studio.

So some help would be nice

1 Like

Does PlayerHasLeft fire with :BindToClose as well? If not you should add it so PlayerHasLeft is fired for both :BindToClose and PlayerRemoving (and adding a debounce so it only saves once). I just mention this as sometimes studio will fire BindToClose before the player has left so PlayerRemoving isn’t fired. But it’s a best guess.

I just made it so that when the game is about to close that this function that saves the data is also fired for everybody that is in the game, and it is working, but it does give me this warning, any idea on what now?
rb4

That’s fine it basically because your saving it twice it’s not a problem

Your gonna want to add debounces. Also with BindToClose you have 30 seconds and because SetAsync() yields, here is some changes

This is an example, with my example function SaveData:

local ListOfPlayers = {}

--My main function

local function SaveData(Player)
if table.find(ListOfPlayers, Player.Name) then return end

   table.insert(ListOfPlayers, Player.Name)

   --Code To Save Data

   repeat task.wait() until game.Players:FindFirstChild(Player.Name) == nil
   table.remove(ListOfPlayers, table.find(ListOfPlayers, Player.Name))
end



---Player Leaving Events

game:BindToClose(function()
	for i, v in game.Players:GetPlayers() do
			coroutine.wrap(SaveData)(v)
		end
	
end)

game.Players.PlayerRemoving:Connect(function(Player)
    coroutine.wrap(SaveData)(Player)
end)

By doing something like above, It will only fire once^

It is fine, The only issue that I have with it is that with limits. for example if the max server size was 10, and I shut down all the servers to update the game, It could be updating it 20 times in a 30 second period which is a lot and will very likely throw an error and not save all requests.

Nice it works, I am still trying to understand everything a little bit since I am not that experienced yet with lua. but thx for taking your time to help me out. :upside_down_face:

1 Like

Sometimes data stores only work outside of Roblox Studio or in a team testing environment.

1 Like

Yeah, I have noticed that since it suddenly just didn’t work anymore, but this solution fixes this problem luckily.

Yea and that is often my because BindToClose is called and disconnects all RBXEvents so when the Player leaves Player Removing isn’t called in studio as you are the only person in studio and BindToClose is called instead of PlayerRemoving. Or atleast that’s what I notice

1 Like

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