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
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
We need to:
-- 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.