Here is my script
CashData = game:GetService("DataStoreService"):GetDataStore("CashData") GemsData = game:GetService("DataStoreService"):GetDataStore("GemsData") game.Players.PlayerAdded:connect(function(plr) local stats = Instance.new('IntValue', plr) stats.Name = 'leaderstats' local cash = Instance.new('IntValue', stats) cash.Name = 'Cash' cash.Value = 0 stats.Name = 'leaderstats' local cash = Instance.new('IntValue', stats) cash.Name = 'Gems' cash.Value = 0 end)
I tried to make it autosave but it wont work, i tried two diffrent ways but this is all ive got
For first remember: You not need to get few datastores, cuz you can save multiple data in one datastore.
local DS = game:GetService("DataStoreService") local StatsData = DS:GetDataStore("StatsData") local prefix = "User_" game.Players.PlayerAdded:Connect(function(player) local Folder = Instance.new("Folder") Folder.Parent = player Folder.Name = "leaderstats" local Gems = Instance.new("IntValue") Gems.Parent = Folder Gems.Name = "Gems" local Cash = Instance.new("IntValue") Cash.Parent = Folder Cash.Name = "Cash" local plrData = StatsData:GetAsync(prefix..player.UserId) if plrData ~= nil then --checking if player have data Gems.Value = plrData[1] --importing data Cash.Value = plrData[2] else -- saving data if player didnt have data StatsData:SetAsync(prefix..player.UserId, { player.leaderstats.Gems.Value; player.leaderstats.Cash.Value; }) end end) game.Players.PlayerRemoving:Connect(function(player) --saves when player leave StatsData:SetAsync(prefix..player.UserId, { player.leaderstats.Gems.Value; player.leaderstats.Cash.Value; }) end)