For some reason, my coins in the game only save whenever they are increased. When they are decreased and I leave the game it loads back at what the coins used to be before they were decreased. I tried a solution to fix this which was save the coins whenever they changed but it didn't work. I also tried playing in Roblox studio and Roblox itself but it didn't work.
Also sometimes the floors dont save.
Server Script
local DataStoreService = game:GetService("DataStoreService") local GameDataStore = DataStoreService:GetDataStore("GameDataStore") local Players = game:GetService("Players") local function leaderboardSetup (player) local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = player local Coins = Instance.new("IntValue") Coins.Name = "Coins" Coins.Value = 0 Coins.Parent = leaderstats local Floors = Instance.new("IntValue") Floors.Name = "Floors" Floors.Value = 0 Floors.Parent = leaderstats local Items = Instance.new("Folder") Items.Name = "Items" Items.Parent = player local SC = Instance.new("IntValue") SC.Name = "SC" SC.Parent = Items SC.Value = 0 local TimeT = Instance.new("IntValue") TimeT.Name = "TimeT" TimeT.Parent = Items TimeT.Value = 0 local data local data2 local success, errormessage = pcall(function() data = GameDataStore:GetAsync(player.UserId.."Floors") data2 = GameDataStore:GetAsync(player.UserId.."Coins") end) if success then Floors.Value = data Coins.Value = data2 print("YAY UR DATA LOADED UWU") else print("NO NO NO DATA DINDT LOAD THIS ISNT GOOD SADYWFDYUWADYAGDWYUIDuwu...") warn(errormessage) end end Players.PlayerAdded:Connect(leaderboardSetup) Players.PlayerRemoving:Connect(function(player) local success, errormessage = pcall(function() GameDataStore:SetAsync(player.UserId.."Floors",player.leaderstats.Floors.Value) GameDataStore:SetAsync(player.UserId.."Coins",player.leaderstats.Coins.Value) end) if success then print("Data saved! OMG SO MUCH UWUW NOISES CONGRATS") else print("OH NO DATA NO SAVED SAD WUYD*AWDGUIWDOAYWDIUYAWD") warn(errormessage) end end)
First of all a question: Why would you use remote events? You could just save them in the Script. Also another tipp: you dont actually have to write all of the parents in these lines local
TimeT = Instance.new("IntValue") 32 TimeT.Name = "TimeT" 33 TimeT.Parent = Items 34 TimeT.Value = 0
you could write this instead TimeT = Instance.new("IntValue",Items) TimeT.Parent = Items
Now to your problem.
Well as far as I can tell you are using the .Changed event of the Values wich only trigger when as the name already says the Value is changed. I would suggest only saving player data when they leave and or in periods of a vew minutes. Saving data each time a Value changes is highly inefficient. If you really care about having data saved at all time then you should check out Datastore2. Now I also suggest to use a BindToClose() function wich triggers saving data in case the server shuts down. Also maybe you should consider using Update async instead of SetAsync since SetAsync is just overwriting the data, wich could be fatal.
so what you could do is to put this in your script (NOT the local script) and just delete the part with the remote event. I also suggest that you follow my advice and make my suggested changes to the script:
Players.PlayerRemoving:Connect(function(player) local success, errormessage = pcall(function() 59 GameDataStore:SetAsync(player.UserId.."Floors",player.leaderstats.Floors.Value) 60 GameDataStore:SetAsync(player.UserId.."Coins",player.leaderstats.Coins.Value) 61 end) 62 63 if success then 64 print("Data saved! OMG SO MUCH UWUW NOISES CONGRATS") 65 else 66 print("OH NO DATA NO SAVED SAD WUYD*AWDGUIWDOAYWDIUYAWD") 67 warn(errormessage) 68 end end)
I hope this helped you out,
kind regards,
esepek