Datastore works fine in-game, but not in studio

to start, yes. studio access to API services is on.

I have one script for everything that saves in game. for example, leader stats, player data and the items in the player’s bag is all added to one table, then the table is saved.

anyways, I noticed that noting saves when the game is inside studio, but saves perfectly when in game. here’s the relevant part of the script.

game.Players.PlayerRemoving:Connect(function(Player)
   local DataForStore = {}
   for i,v in pairs(Player.leaderstats:GetChildren()) do
	   DataForStore[v.Name] = v.Value
   end
   for i,v in pairs(Player.Data:GetChildren()) do
	   DataForStore[v.Name] = v.Value
   end
   for i,v in pairs(Player.Berries:GetChildren()) do
	   DataForStore[v.Name] = v.Value
   end
   for i,v in pairs(Player.BagsOwned:GetChildren()) do
	   DataForStore[v.Name] = v.Value
   end
   local s = pcall(Datastore.SetAsync, Datastore, "uid-" .. Player.UserId, DataForStore)

   if s then print("Saved data for " .. Player.Name) end
end)

it doesn’t print that it saved the data for the player in studio, but it does in game. here’s me kicking my friend, and his data saved
image

if any more details are needed, I’m happy to provide

1 Like

This might be a strange question, but how are you testing this? Are you running it from “Play,” or from a local server with local clients?

the play button in studioimage

That could be the problem, as I’ve run into it before, too. I think the simulation stops running before the call gets made. Try it with a local server and a client, then disconnect the client without stopping the server or pressing the “cleanup” button and see if it works.

yeah that worked, should I just do that from now on?

sorry for the wait btw

Yeah, if you need to test DataStores when a player leaves, that’ll likely be what you need to do. If your scripts autosave during gameplay, though, they should work as long as they make their calls while the game is still running even in “Play” mode.

1 Like

Supposedly, while testing this in Studio the simulation while running “Play Solo” & stopping it could result in the PlayerRemoving event not firing entirely

A fix for this would be to use a BindToClose() function, which would fire before the server starts to shutdown (Provided if given a time limit)

local function SaveData(Player)
   local DataForStore = {}
   for i,v in pairs(Player.leaderstats:GetChildren()) do
	   DataForStore[v.Name] = v.Value
   end
   for i,v in pairs(Player.Data:GetChildren()) do
	   DataForStore[v.Name] = v.Value
   end
   for i,v in pairs(Player.Berries:GetChildren()) do
	   DataForStore[v.Name] = v.Value
   end
   for i,v in pairs(Player.BagsOwned:GetChildren()) do
	   DataForStore[v.Name] = v.Value
   end
   local s = pcall(Datastore.SetAsync, Datastore, "uid-" .. Player.UserId, DataForStore)

   if s then print("Saved data for " .. Player.Name) end
end)

game.Players.PlayerRemoving:Connect(SaveData)

game:BindToClose(function()
    for _, Player in pairs(game.Players:GetPlayers()) do
        SaveData(Player)
    end
end)

Try this?

there was a small syntax error that I fixed.

it works, but fills the que.
image
this didn’t happen before and there is nothing else that uses datastore in the game currently

@JackscarIitt that’s weird actually, it only happens sometimes

It shouldn’t be too much of an issue if you’re only testing it in Studio

BindToClose() would fire if the server “unexpectedly” shutdown, which would yield first (At a max of 30 seconds) before shutting down

Testing it in-game should work just fine

1 Like