I am trying to make a script that saves my currency “PawCoins”, but every time I leave and come back it resets the leaderstats back to zero. I have studio API access on.
Here is the script I am currently trying to use. It is a tutorial script because I am new to scripting. Ignore the comments I use those to keep track of what does what because I’m forgetful.
-- saves coin data
local DataStoreService = game:GetService("DataStoreService")
local BeachsideDataStore = DataStoreService:GetDataStore("BeachsideDataStore")
game.Players.PlayerAdded:Connect(function(Player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = Player
local PawCoins = Instance.new("IntValue")
PawCoins.Name = "PawCoins"
PawCoins.Parent = leaderstats
local data
local success, errormessage = pcall(function()
BeachsideDataStore:GetAsync(Player.UserId.."-PawCoins",Player.leaderstats.PawCoins.Value)
end)
if success then
PawCoins.Value = data
else
print("There was an error while getting your data")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
local success, errormessage = pcall(function()
BeachsideDataStore:SetAsync(Player.UserId.."-PawCoins",Player.leaderstats.PawCoins.Value)
end)
if success then -- if data saved successfully
print("Player Data Successfully Saved!")
else -- if not
print("There Was An Error When Saving Data")
warn(errormessage)
end
end)
I also tried adding this onto the end:
game:BindToClose(function()
--when game is ready to shutdown
for i, player in pairs(game.Players:GetPlayers()) do
if player then
player:Kick("The game is shutting down, come back soon!")
end
end
local success, errormessage = pcall(function()
BeachsideDataStore:SetAsync(Player.UserId.."-PawCoins",Player.leaderstats.PawCoins.Value)
end)
if success then
print("Closing data saved successfully!")
end
wait(5)
end)
I ended up removing this because it didn’t even work on this script.