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

How can i make something save until the player joins back?

Asked by 2 years ago

I'm trying to create a currency system where it gives you the amount of coins you entered + your existing ones, so say i put in like 54 the textlabel's text would do 0+54 and read 54 but then when you leave and re-join its back to 0

the ammount you have is put in is:

game.starterGui.SquidCoins.Frame.TextLabel.Text

where you put how many coins you want is in:

game.starterGui.SquidCoins.TextBox.Text

1 answer

Log in to vote
0
Answered by 2 years ago

For this you need to use Datastores. Put this in a normal script in ServerScriptService:

local DataStoreService = game:GetService("DataStoreService")

local experienceStore = DataStoreService:GetDataStore("SquidCoins")

game.Players.PlayerRemoving:Connect(function(plr) --When a player is leaving
    local success, errorMessage = pcall(function()
        experienceStore:SetAsync(plr.UserId, plr.PlayerGui.SquidCoins.Frame.TextLabel.Text)
    end)
    --It saves data when a player is leaving
    if not success then
        print(errorMessage)
    end
end)

game.Players.PlayerAdded:Connect(function(plr) --When a player joins
    local success, currentExperience = pcall(function()
        return experienceStore:GetAsync(plr.UserId)
    end)
    --it loads saved data when player joins again
    if success then
        plr.PlayerGui.SquidCoins.Frame.TextLabel.Text = currentExperience
    end
end)

Know more about Datastores here: Datastores - Roblox Developer Hub

Ad

Answer this question