Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

How can I make Kills and Wipeouts datasave?

Asked by 14 days ago

I've been trying to do this for about 2 days, and I can't find any good tutorials on youtube or any other platform. Maybe some of you can help me make this work?

My script:

local Players = game.Players


local Template = Instance.new 'BoolValue'
Template.Name = 'leaderstats'

Instance.new('IntValue', Template).Name = "Kills"
Instance.new('IntValue', Template).Name = "Deaths"


Players.PlayerAdded:connect(function(Player)
    wait(1)
    local Stats = Template:Clone()
    Stats.Parent = Player
    local Deaths = Stats.Deaths
    Player.CharacterAdded:connect(function(Character)
        Deaths.Value = Deaths.Value + 1
        local Humanoid = Character:FindFirstChild "Humanoid"
        if Humanoid then
            Humanoid.Died:connect(function()
                for i, Child in pairs(Humanoid:GetChildren()) do
                    if Child:IsA('ObjectValue') and Child.Value and Child.Value:IsA('Player') then
                        local Killer = Child.Value
                        if Killer:FindFirstChild 'leaderstats' and Killer.leaderstats:FindFirstChild "Kills" then
                            local Kills = Killer.leaderstats.Kills
                            Kills.Value = Kills.Value + 1
                        end
                        return
                    end
                end
            end)
        end
    end)
end)

1 answer

Log in to vote
-1
Answered by
Sparks 534 Moderation Voter
14 days ago
Edited 14 days ago

You are on the right track. When saving player data, you will want to use DataStores. DataStores allow you to persist data across existing and new servers of a game.

Before you get any deeper, you should know that DataStores have limits on how many requests you are allowed make per minute. Therefore, it is important to manage your resources effectively. There are usually at least these three instances where you want to save data, and all three apply to your use case:

  1. Player is leaving;
  2. The DataModel is closing (server shutting down)
  3. Auto-save - just to be safe, autosave player data every 1-15 minutes;

DataStore values are accessed by keys unique per store. A common way to save data is to take the Player's UserId, and append anything to it if necessary. For your case, I will make a table of data per entry.

I will outline your use cases below:

local DataStoreService = game:GetService("DataStoreService")
local playerstats = DataStore:GetDataStore("KillsDeaths")
local Players = game:GetService("Players")

-- First, you will want to check if the entry for the user exists (or new user). 
Players.PlayerAdded:Connect(function(player)
    -- Read the linked wiki aritcle to answer "why pcall?"
    local success, stats = pcall(function()
        return playerstats:GetAsync(player.UserId)
    end

    if success then
        if stats == nil then
            playerstats:SetAsync(player.UserId, {0, 0})
        end

    else
        --Handle failed datastore requests here. You should probably use a logging service for this.
    end
end)



-- 1. Player leaving
Players.PlayerRemoving:Connect(function(player)
    local stats = {player.leaderstats.Kills.Value, player.leaderstats.Deaths.Value}

    local success, newstats = pcall(function()
        return UpdateAsync(player.UserId, function(olddata)
            olddata[1] += stats[1]
            olddata[2] += stats[2]
            return olddata
        end)
    end

    if success then
        print("new stats:", table.unpack(newstats))
    else
        --Handle failed datastore requests here.
    end

    -- 3. Autosave
    while true do wait(60)
        local success, newstats = pcall(function()
            return UpdateAsync(player.UserId, function(olddata)
                olddata[1] += stats[1]
                olddata[2] += stats[2]
                return olddata
            end)
        end

        if success then
            print("new stats:", table.unpack(newstats))
        else
            --Handle failed datastore requests here.
        end
    end
end)



-- 2. Game closing
game:BindToClose(function()
    for i,v in pairs(Players:GetPlayers()) do
        local stats = {v.leaderstats.Kills.Value, v.leaderstats.Deaths.Value}
    -- Might want to a queue to avoid exhausting DS rate limit
        return UpdateAsync(player.UserId, function(olddata)
            olddata[1] += stats[1]
            olddata[2] += stats[2]
            return olddata
        end)

        if success then
            print("new stats:", table.unpack(newstats))
        else
            --Handle failed datastore requests here.
        end
    end
end)

It is also a good idea to modularize your code using ModuleScripts. In this case, you might want to write a module for saving/getting player kills/deaths so you don't have to repeat code wherever you want to update it.

0
unsafe data storing iuclds 550 — 14d
0
wdym unsafe, it has all the pcalls and needed saves, maybe i am wrong i am not good at them but i would use this, how do you make it 'safe' then? imKirda 731 — 14d
Ad

Answer this question