I know how to call a function when a player joins the game, but how do I save an IntValue to a character so they can save their amount of something and keep that amount whenever they join back? (For example, an amount of money being 100 and saving it for the next time they get back on the world) Here's what I have right now: game.Players.PlayerAdded:Connect(function(plr)
end)
btw, this script is in my Workspace not ServerScriptService, if that matters.
Simply, use a script in ServerScriptService to load their value when the player joins the game, and saves the value when the player leaves the game, like so:
local dss = game:GetService("DataStoreService") local myservice = dss:GetDataStore("Cash") game.Players.PlayerAdded:Connect(function(client) local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = client local cash = Instance.new("IntValue") local getCash = myservice:GetAsync(client.UserId.."-Cash") cash.Name = "Cash" cash.Value = getCash cash.Parent = leaderstats end) game.Players.PlayerRemoving:Connect(function(client) myservice:SetAsync(client.UserId.."-Cash", client.leaderstats.Cash.Value) end)
Make sure that API is enabled on your game. Otherwise, it will print out an error.