Hi, I've been wondering why none of my leaderstats would save anything. I've tried multiple scripts, taken lots from different YouTube videos, lots of websites, and many models. Still doesn't work. Yes, I have enabled studio access to API services! The script is in the folder "ServerScriptService"
Here is the script:
(Taken from this video: https://www.youtube.com/watch?v=lS20iM7iRQE&t) (I was going to change "Gems" and "Cash" to kills, and deaths, but this didn't work first..)
`local datastore = game:GetService("DataStoreService") local ds1 = datastore:GetDataStore("GemSaveSystem") local ds2 = datastore:GetDataStore("CashSaveSystem")
game.Players.PlayerAdded:connect(function(plr) local folder = Instance.new("Folder", plr) folder.Name = "leaderstats" local gems = Instance.new("IntValue", folder) gems.Name = "Gems" local cash = Instance.new("IntValue", folder) cash.Name = "Cash"
gems.Value = ds1:GetAsync(plr.UserId) or 0 ds1:SetAsync(plr.UserId, gems.Value)
cash.Value = ds2:GetAsync(plr.UserId) or 0 ds2:SetAsync(plr.UserId, cash.Value)
gems.Changed:connect(function() ds1:SetAsync(plr.UserId, gems.Value) end)
cash.Changed:connect(function() ds2:SetAsync(plr.UserId, cash.Value) end) end) `
Your script is working, I copied and pasted it in my game and it worked... you have no problems at all.
If there was anything wrong with it, here's your script that I modified (and tested) to make it look more neat:
local datastore = game:GetService("DataStoreService") local ds1 = datastore:GetDataStore("SaveSystem") -- Changed name, change back to "GemSaveSystem" if others were playing your game so their values are accessible game.Players.PlayerAdded:connect(function(plr) local folder = Instance.new("Folder", plr) folder.Name = "leaderstats" local gems = Instance.new("IntValue", folder) gems.Name = "Gems" local cash = Instance.new("IntValue", folder) cash.Name = "Cash" gems.Value = ds1:GetAsync(plr.UserId) or 0 ds1:SetAsync(plr.UserId.."-Gems", gems.Value) cash.Value = ds1:GetAsync(plr.UserId) or 0 ds1:SetAsync(plr.UserId.."-Cash", cash.Value) gems.Changed:connect(function() ds1:SetAsync(plr.UserId.."-Gems", gems.Value) end) cash.Changed:connect(function() ds1:SetAsync(plr.UserId.."-Cash", cash.Value) end) end)
Again, if a lot of players were playing this game, CHANGE "ds1" NAME BACK TO "GemsSaveSystem"!!! (line 2)