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?

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

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.

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