local service = game:GetService("DataStoreService")
local ds = service:GetDataStore("DS")
game.Players.PlayerAdded:Connect(function(plr)
local id = plr.UserId
local folder = Instance.new("Folder")
folder.Name = "leaderstats"
folder.Parent = plr
local int = Instance.new("IntValue")
int.Name = "Cash"
int.Parent = folder
local data
local s, e = pcall(function()
data = ds:GetAsync(id)
print("loading")
end)
if s then
int.Value = data
else
warn("error to load.")
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local id = plr.UserId
local data = {
Cash = plr.leaderstats.Cash.Value;
}
local success, error = pcall(function()
ds:SetAsync(id,data)
end)
if success then
print("success!")
else
warn("failed to save.")
end
end)
Whenever i leave the game, it doesn’t print out the result. Help!
Are you testing this on play solo? I think you should test this in a local server or a real roblox server. And you also need to enable studio access to API services in the game settings, if you test it in studio, but you probably did that already. I’m not sure, but I believe you should also save players’ data when the game server closes using game:BindToClose().
Turn on the API mode in configure games or place, this should work.
Side Note Edit: it’s ironic that you just answered someone’s question about their datastore not loading in and you just made the same post but different sentences i guess.
After change playerRemoving for game:BindToClose() (BindToClose are better like, if your shutdown your game for whatever reason this will call BindToClose and playerRemoving don’t) and try it on roblox becose on studio the data saving have by disabled so this is normally giving you a error that you can avoid by seeing if the game is in the studio and if it is, don’t even try to save https://developer.roblox.com/en-us/api-reference/function/RunService/IsStudio
Here a small exemple
if not game:GetService("RunService"):IsStudio() then
for i, player in pairs(game.Players:GetPlayers()) do
saveValues(player)
end
else
warn("Stage not save Studio Detected")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
if not game:GetService("RunService"):IsStudio() then
saveValues(player)
else
warn("Stage not save Studio Detected")
end
end)
As for saving try a script in this style
local function saveValues(player)
local saveData = {}
local StatsFile = player.leaderstats
for i,v in pairs(StatsFile:GetChildren()) do
saveData[#saveData + 1] = v.Name
saveData[#saveData + 1] = v.Value
end
ds:SetAsync(player.UserId, saveData)
end
@RoBoPoJu and @reygenne1 we have already listed some element of what I just said
Also, there is DataStore2 which was created by (I can’t find the source so I won’t say anything)
Ok now it works, but it only saves my data while I am in studio. When I tried to play the game, it doesn’t save my data there.
This is the code now,
local DSS = game:GetService("DataStoreService")
local ds = DSS:GetDataStore("CashAndWins")
local run = game:GetService("RunService")
game.Players.PlayerAdded:Connect(function(plr)
local folder = Instance.new("Folder",plr)
folder.Name = "leaderstats"
local cash = Instance.new("IntValue",folder)
cash.Name = "Cash"
local id = plr.UserId
local data
local success, errormessage = pcall(function()
data = ds:GetAsync(id)
end)
if success then
print("Successfully obtain data!")
cash.Value = data
else
warn("Failed to load ata.")
end
end)
game:BindToClose(function()
print("hi")
if not run:IsStudio() then
for i,v in pairs(game.Players:GetChildren()) do
local data = v.leaderstats.Cash.Value
local success, errormessage = pcall(function()
ds:SetAsync(v.UserId,data)
end)
if success then
print("yes")
end
end
end
end)
Why are you using BindToClose, but not PlayerRemoving event? BindToClose runs when the game is going to shut down, PlayerRemoving runs when a player is going to leave the game.
This thread here is really useful as you seem to be pretty confused in what both of them do specifically:
We here on the developer forum, can only give the advice/solution, writing the script is actually your work after understanding what we say.
So everything that I can do is guide you, I would say you should use the player removing function and the BindToClose event for saving, and also have a look at the thread I gave in my last reply and as a side note always Ensure that API Services are turned on for the DataStores to work in studio.
I don’t think you are understanding the thing, or maybe my explanation is bad. I will try once more:
What the PlayerRemoving Event Does:
The PlayerRemoving event fires right before a Player leaves the game. So suppose you join a game, and then leave 2 minutes later, this event will be fired, until and unless you were the last player to leave, or the developers shut down the game or some network error was faced and the server was shut downed
What the BindToClose Event Does:
This event function is run prior to the game server being shut downed not when a player leaves, so for example: if a player leaves and there are still players in the server this event would not be run. So this is useful when the game Server shutdowns for some reason / The last player leaves the game.
Sorry, ive looked at the message wrongly, but however, even though I used PlayerRemoving event to save a player’s data, it still doesn’t work. I’ve made sure I spelt correctly. Is it because I’m in studio? Will it work when I’m playing in the game or Will it work as well if I play in studio?