Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to save IntValue data to a player?

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.

2 comments

0

You would want to use data stores.

0

itz_rennox thx! That link was super-useful!

1 answer

✓ Accepted answer

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.