Hey there,
I have this leaderstats script that’s been working fine but just recently it stopped saving. I’ve tried finding the problem through the print function but they worked but the leaderstats still did not save. Anybody see the problem?
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("SkinStats")
game.Players.PlayerAdded:Connect(function(Player)
local Leaderstats = Instance.new("Folder", Player)
Leaderstats.Name = "leaderstats"
local Cash= Instance.new("IntValue", Leaderstats)
Cash.Name = "Cash"
Cash.Value = 0
local Kills= Instance.new("IntValue", Leaderstats)
Kills.Name = "Kills"
Kills.Value = 0
local Data = DataStore:GetAsync(Player.UserId)
if Data then
Cash.Value = Data.Cash
Kills.Value = Data.Kills
print("also works")
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
DataStore:SetAsync(Player.UserId, {
["Cash"] = Player.leaderstats.Cash.Value;
["Kills"] = Player.leaderstats.Kills.Value;
})
end)
print("works")
Following with this suggestion, you could bind a wait function to run when the game closes so that the server has time to handle the datastore (and possibly other things)
Okay. @D0RYU already said it, but try using the BindToClose event (in any script) in order to allow the server more time to run scripts once the game is closed.
Edit: Ask me if you need me to elaborate on what you should put in the function
game.Players.PlayerAdded:Connect(function(Player)
local Leaderstats = Instance.new("Folder", Player)
Leaderstats.Name = "leaderstats"
local Cash= Instance.new("IntValue", Leaderstats)
Cash.Name = "Cash"
local Kills= Instance.new("IntValue", Leaderstats)
Kills.Name = "Kills"
local Data
local got,Newplayer = pcall(function()
Data = DataStore:GetAsync(Player.UserId)
end)
if got and Data then
Cash.Value = Data.Cash
Kills.Value = Data.Kills
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
local Success,Error = pcall(function()
DataStore:SetAsync(Player.UserId, {
Cash = Player.leaderstats.Cash.Value,
Kills = Player.leaderstats.Kills.Value
})
end)
if not Success then warn(Error) end
end)
game:BindToClose(function()
if game:GetService("RunService"):IsStudio() then
wait(1)
else
for _, v in pairs(game:GetService("Players"):GetPlayers()) do
local Success,Error = pcall(function()
DataStore:SetAsync(v.UserId,{
Kills = v.leaderstats.Kills.Value,
Cash = v.leaderstats.Cash.Value
})
end)
if not Success then
warn(Error)
end
end
end
end)
This is the Fixed script.
Also, Can you show Where you Define dataStore?
Side question for you. What difference does the spaces between commas do for a script? I’m assuming it is just for better looks and organization, but it doesn’t have any effect does it?
it doesn’t change the functionality of the script at all, but it is good practice to add spaces because readability is much better.
also as you work with them more and more you start to understand why spaces are so helpful when reading your code or someone else’s code
Hey, I fixed it but in a different way, I had a script that was identical to that script but with different a datastore name and not called leaderstats to save a different value and once I deleted that it worked.
Sorry, I didn’t see this in my notifications. Saving a bool is the same method as saving a number, or a string. There is nothing you need to do differently, other than defining whether it is true or false.