Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

I've been trying to do this all day, and it's just not getting into my head. Could somebody please help me?

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 -- Only one player can get a KO for killing a player. Not 2, not 3. Only one.
					end
				end
			end)
		end
	end)
end)
-- Coded by JulienDethurens using gedit.

2 answers

Like Puppy44 said, use DataStoreService Here's an example:

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("GameStats")
 
game.Players.PlayerAdded:Connect(function(Player)
    local Leaderstats = Instance.new("Folder", Player)
    Leaderstats.Name = "leaderstats"
    local death = Instance.new("IntValue", Leaderstats)
    death.Name = "Deaths"
    death.Value = 0
    local kills = Instance.new("IntValue", Leaderstats)
    kills.Name = "Kills"
    kills.Value = 0
    local Data = DataStore:GetAsync(Player.UserId)
    if Data then
        kills.Value = Data
    end
end)
 
game.Players.PlayerRemoving:Connect(function(Player)
    DataStore:SetAsync(Player.UserId, Player.leaderstats.Kills.Value)
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.