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

How do I put a data persistence in a value of a player?

Asked by 10 years ago

In a player on my game, there's a value called Rank(string value) in game.Players.PlayerName

How do I make it so the string is saved and will appear as the same string next time they join a server

1 answer

Log in to vote
4
Answered by 10 years ago

You can use the DataStoreService.

Updated to save when the value is changed.

local DataStore = Game:GetService("DataStoreService"):GetDataStore("Rank")

local Requests = 0
local Max  = 60

function Good()
    return Requests <= Max
end

function UpdateMax()
    Max = 60 + 10 * #Game.Players:GetPlayers()
end


Game.Players.PlayerAdded:connect(function(Player)
    local Stat = Player:WaitForChild("leaderstats"):WaitForChild("Rank")
    Stat.Value = DataStore:GetAsync(Player.userId)
    Stat.Changed:connect(function()
        if Good() then
            DataStore:SetAsync(Player.userId,Stat.Value)
            Requests = Requests + 1
        end
    end)
end)
Game.Players.PlayerRemoving:connect(function()
    UpdateMax()
end)

Spawn(function()
    while wait(60) do
        Requests = 0
    end
end)
0
I think you should save the value everytime the value changes. That way you have less of a chance of losing your data. TheGuyWithAShortName 673 — 10y
Ad

Answer this question