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

How can I make a leaderboard that saves your amount of kills?

Asked by 10 years ago

Hey

I need to make a leaderboard that only counts kills, and saves the kills if you leave game.

How do I do this?

Thanks

1 answer

Log in to vote
0
Answered by
jobro13 980 Moderation Voter
10 years ago

We need to:

  • Get the Player
  • Setup the initial leader stat value
  • When changed, update it in the data store
-- Create / Get a Data Store for Player Kills
local DataStore = game:GetService("DataStoreService"):GetDataStore("PlayerKills")

-- Save a number
function SaveNumber(userid, ammount)
-- We would normally use UpdateAsync but in this case due it is per-player data we can also use SetAsync
DataStore:SetAsync(tostring(userid), ammount)
end

--Setup leaderstats
function create_stats(player)
    if player:FindFirstChild("leaderstats") then
        player.leaderstats:Destroy()
    end
    local mod = Instance.new("Model", player)
    mod.Name = "leaderstats"
    local kills = Instance.new("NumberValue", mod)
    kills.Name = "Kills"
    kills.Value = DataStore:GetAsync(tostring(userid)) or 0
    return kills
end

-- Do this when a player joins
game.Players.PlayerAdded:connect(function(player)
    local kill_value = create_stats(player)
    kill_value.Changed:connect(function(NewValue)
        SaveNumber(player.userId, NewValue)
    end)

Note: This is a server script.

Please comment if you don't understand something.

Ad

Answer this question