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?
I believe if you have created the PlayerRemoving and BindToClose functions properly, it should work. Could you post your lastest code after you make the changes?
The problem is that you just set the value to a dictionary containing the values, not the saved value it self. Since you save cash inside data, you would have to index cash inside the dictionary since dictionary are key-value pairs.
int.Value = data.Cash
If you print data, it would print the memory address at where it is located.