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

How to save data at regular intervals?

Asked by 2 years ago

As you can see in my leaderstats script I am saving my data after player leaves, which is highly risky and can cause data loss, I am trying to save data at regular intervals but problem is that if I will make while loop here then everything will be interrupted, isn't it? so then how can I save data at regular intervals?

local dataStoreService = game:GetService("DataStoreService")
local playerData = dataStoreService:GetDataStore("PlayerData")

game.Players.PlayerAdded:Connect(function(player)

    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player
    local kills = Instance.new("IntValue")
    kills.Parent = leaderstats
    kills.Name = "Kills"

    local Experience = Instance.new("StringValue")
    Experience.Parent = leaderstats
    Experience.Name = "Experience"


    local SurvivalTime = Instance.new("IntValue")
    SurvivalTime.Parent = leaderstats
    SurvivalTime.Name = "Survived"

    local Money = Instance.new("IntValue")
    Money.Parent = leaderstats
    Money.Name = "BackDollars"  

    player.CharacterAdded:Connect(function(character)
        local humanoid = character:FindFirstChild("Humanoid")

            humanoid.Died:Connect(function(died)
                local tag = humanoid:FindFirstChild("creator")
                local killer = tag.Value
                if tag and killer   then
                    killer.leaderstats:FindFirstChild("Kills").Value = killer.leaderstats:FindFirstChild("Kills").Value + 1
                    killer.leaderstats:FindFirstChild("BackDollars").Value += 1
                end
            end)
    end)
    local playerUserId = "Player_"..player.UserId

    local data = playerData:GetAsync(playerUserId)
    if data then
        kills.Value = data["Kills"]
        Experience.Value = data["Experience"]
        SurvivalTime.Value = data["Survived"]
        Money.Value = data["BackDollars"]
    else
        kills.Value = 0
        Experience.Value = "Newcomer"
        SurvivalTime.Value = 0
        Money.Value = 0
        print("datastore")
    end
end)

local function create_table(player)
    local player_stats = {}
    for i, stat in pairs(player.leaderstats:GetChildren()) do
        player_stats[stat.Name] = stat.Value
    end
    return player_stats
end

local function onPlayerExit(player)
    local player_stats = create_table(player)
    local success, err = pcall(function()
        local playerUserId = "Player_"..player.UserId
        playerData:SetAsync(playerUserId, player_stats)

    end)

    if not success then
        warn("Could not save data")
    end
end


game.Players.PlayerRemoving:Connect(onPlayerExit)

0
I have no idea what are you talking about. It works (possibly). ryanzhzh 128 — 2y

2 answers

Log in to vote
1
Answered by
MattVSNNL 620 Moderation Voter
2 years ago

Here ya go! : D

local PlayersService = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")

local currencyDataStore = dataStoreService:GetDataStore("CurrencyDataStore")

PlayersService.PlayerAdded:Connect(function(player)

    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local kills = Instance.new("NumberValue")
    kills.Name = "Kills"
    kills.Parent = leaderstats

    local Experience = Instance.new("StringValue")
    Experience.Name = "Experience"
    Experience.Parent = leaderstats

    local SurvivalTime = Instance.new("NumberValue")
    SurvivalTime.Name = "Survived"
    SurvivalTime.Parent = leaderstats

    local Money = Instance.new("NumberValue")
    Money.Name = "BackDollars"  
    Money.Parent = leaderstats

    local killsData = nil
    local experienceData = nil
    local survivalData = nil
    local moneyData = nil

    pcall(function()
        killsData = currencyDataStore:GetAsync(player.UserId.."-kills")
    end)

    pcall(function()
        experienceData = currencyDataStore:GetAsync(player.UserId.."-experience")
    end)

    pcall(function()
        survivalData = currencyDataStore:GetAsync(player.UserId.."-survival")
    end)

    pcall(function()
        moneyData = currencyDataStore:GetAsync(player.UserId.."-money")
    end)

    if killsData ~= nil then

        kills.Value = killsData
    end

    if experienceData ~= nil then

        Experience.Value = experienceData
    end

    if survivalData ~= nil then

        SurvivalTime.Value = survivalData
    end

    if moneyData ~= nil then

        Money.Value = moneyData
    end

end)

local bindableEvent = Instance.new("BindableEvent")

PlayersService.PlayerRemoving:Connect(function(player)

    pcall(function()
        currencyDataStore:SetAsync(player.UserId.."-kills", player.leaderstats.Kills.Value)
    end)

    pcall(function()
        currencyDataStore:SetAsync(player.UserId.."-experience", player.leaderstats.Experience.Value)
    end)

    pcall(function()
        currencyDataStore:SetAsync(player.UserId.."-survival", player.leaderstats.Survived.Value)
    end)

    pcall(function()
        currencyDataStore:SetAsync(player.UserId.."-money", player.leaderstats.BackDollars.Value)
    end)

    bindableEvent:Fire()
    print("Saved!")

end)

game:BindToClose(function()
    while #PlayersService:GetPlayers() > 0 do
        bindableEvent.Event:Wait()
        wait()
    end
end)
0
Thank you bro, I really appreciate it :) Sabailuridze 126 — 2y
1
No problem! : D MattVSNNL 620 — 2y
Ad
Log in to vote
0
Answered by
enes223 327 Moderation Voter
2 years ago

hey you! have you ever heard of enes? if you are in trouble, better call enes!

Answer this question