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
if any more details are needed, I’m happy to provide
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, 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.
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)