Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to save IntValue data to a player?

Asked by 4 years ago

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.

0
You would want to use data stores. DeceptiveCaster 3761 — 4y
0
itz_rennox thx! That link was super-useful! User#28017 0 — 4y

1 answer

Log in to vote
1
Answered by 4 years ago

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.

Ad

Answer this question