I create this script , and put it in the "ServerScript Service But when i change the Value Cash1, it wont save it. I call the Cash from a localscript in Screen Gui When i start the Server again the Cash.Value is 0 ....
local player = game.Players.LocalPlayer local levelpoint = player.leaderstats.Cash1
local DataStoreServer = game:GetService("DataStoreService") local myDataStore = DataStoreServer:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(player) local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = player
local Cash1 = Instance.new("IntValue") Cash1.Name = "Cash1" Cash1.Parent = leaderstats local data local succes, errormessage = pcall(function() data = myDataStore:GetAsync(player.UserId.."Cash1") end) if succes then Cash1.Value = data else print("there was an error ") warn(errormessage) end
end) game.Players.PlayerRemoving:Connect(function(player) local succes , errormessage = pcall(function() myDataStore:SetAsync(player.UserId.."Cash1",player.leaderstats.Cash1.Value)
end) if succes then print("player data succesfully saved") warn(errormessage) end
end)
Script in ServerScriptService!
local dataStoreService = game:GetService('DataStoreService') local DS = dataStoreService:GetDataStore('Cash') game.Players.PlayerAdded:Connect(function(plr) local leaderstats = Instance.new('Folder') leaderstats.Name = 'leaderstats' leaderstats.Parent = plr local currency = Instance.new('IntValue') currency.Name = 'Cash' currency.Value = DS:GetAsync(plr.UserId) or 0 currency.Parent = leaderstats currency.Changed:Connect(function() DS:SetAsync(plr.UserId, currency.Value) end) end)
If you publish game, click 'Game Settings', in 'Options' enable Studio Access to API Services (click ON)! Can autosave leaderstats :) Try in console log
game.Players."Your Username".leaderstats.Cash.Value = 5