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

How do I autosave data instead of saving only when a player leaves?

Asked by 8 years ago
Edited 8 years ago

So I have a script which saves player data whenever the player leaves. But I would want to make it so that it autosaves the data every few seconds. Because when a player leaves, I would be able to call on a function with the parameter of that specific player.

But if I were to make a loop, I will not be able to get each specific name of the players.

I have thought of making a 'LocalScript' which would be imported into every player when they join. And then I would be able to use game.Players.LocalPlayer to get the specific player. But I want to see if there are other methods of doing this.

My original script is here:

local DSService = game:GetService("DataStoreService"):GetDataStore("DataSaving832723579985723")

game.Players.PlayerAdded:connect(function(player)
    local playerId = "ID-"..player.userId

    local leaderstats = Instance.new("IntValue", player)
    leaderstats.Name = "leaderstats"

    local money = Instance.new("IntValue", leaderstats)
    money.Name = "Money"
    money.Value = 0

    local exp = Instance.new("IntValue", leaderstats)
    exp.Name = "Experience"
    exp.Value = 0

    local level = Instance.new("IntValue", leaderstats)
    level.Name = "Level"
    level.Value = 0

    exp.Changed:connect(function()
        level.Value = math.floor(exp.Value / 1000)
    end)

    local HasSaved = DSService:GetAsync(playerId)

    if HasSaved then
        money.Value = HasSaved[1]
        exp.Value = HasSaved[2]
        level.Value = HasSaved[3]
    else
        local toSave = {money.Value, exp.Value, level.Value}
        DSService:SetAsync(playerId, toSave)
    end

end)

game.Players.PlayerRemoving:connect(function(player)
    local playerId = "ID-"..player.userId
    local Save = {player.leaderstats.Money.Value, player.leaderstats.Experience.Value, player.leaderstats.Level.Value}
    DSService:SetAsync(playerId, Save)
end)

I hope someone can give me another idea for this.

1 answer

Log in to vote
1
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
8 years ago
Edited 8 years ago

Datastores can only be used in ServerScripts so LocalScripts are out of the question (unless you want to use remote events, but I suggest against that in this case).

As for loops, this is the only way to repeated calls infinitely (as you can only recurse so deep before you hit a stack overflow), so we will have to use some sort of looping structure. I suggest have a while loop that repeats every x amount of time in which you loop through all the players in the game and save their data.

while wait(5) do
    for _, player in ipairs(game.Players:GetPlayers()) do
        datastore:SetAsync("ID-"..player.userId, {
            player.leaderstats.Money.Value,
            player.leaderstats.Experience.Value,
            player.leaderstats.Level.Value
        })
    end
end
Ad

Answer this question