Leaderboard System For Kills And Deaths

Hi so i have a leaderboard for kills and deaths and for some reason when ever i die or get kills the leaderboard on the part does not update

local DataStore = game:GetService('DataStoreService')
local OrderedDataStore = DataStore:GetOrderedDataStore('KillsLB2')
local cache = require(game.ServerScriptService["Server/Data"].ProfileCacher)

local refreshTime = 2

local ResetTimer = script.Parent.Parent.ResetTimer

while true do
	wait(1)
	refreshTime -= 1
	ResetTimer.Text = "Leaderboard refreshes in " .. refreshTime .. " seconds!"

	if refreshTime == 0 then
		refreshTime = 60

		for index, plr in pairs(game.Players:GetChildren()) do
			local profile = cache[plr]
			if profile ~= nil then
				OrderedDataStore:SetAsync(plr.UserId, profile.Data.Kills)
			end
		end

		for index, list in pairs(script.Parent:GetChildren()) do
			if list.ClassName == 'Frame' then
				list:Destroy()
			end
		end

		local success, errorMessage = pcall(function()

			local data = OrderedDataStore:GetSortedAsync(false, 40)
			local currentPage = data:GetCurrentPage()

			for currentRank, storedData in ipairs(currentPage) do

				local name = game.Players:GetNameFromUserIdAsync(tonumber(storedData.key))
				local time = storedData.value

				local cloneList = script.Template:Clone()
				cloneList.Name = name .. " 's leaderboard rank"
				cloneList.Username.Text = name
				cloneList.Rank.Text = currentRank
		
				cloneList.Amount.Text = time
				cloneList.Parent = script.Parent


			end
		end)
	end
end
2 Likes

make it Instance.new(“Folder”) it needs to be an folder to work.

1 Like

ok i will try that thank you. char limit

1 Like

i edited the script would this work

1 Like

i made new one for only kills im more focused on kills right now

1 Like

also does the kills get saved after you leave or when you get the kill?

2 Likes

no they dont im working on that but i want the leaderboard to work first by the way when i say leaderboard i dont mean built in one i mean one that you would find in a simulator but for kills

1 Like

yes you need to make the datastore update when you get the kill else it wont update the stats board in game

1 Like

im so confused im pretty new to scripting and dont get what you mean

1 Like

would it be possible to find like a model for a coins leaderboard and change all code for coins to kills?

1 Like

probably or just copy the code what you have right now and change datastore names etc.

ok i will try that thanks for the help!

2 Likes

would this work?

local ds = game:GetService("DataStoreService")

local KillsODS = ds:GetOrderedDataStore("KillsStats")


local timeUntilReset = 10


while wait(1) do
	
	
	timeUntilReset = timeUntilReset - 1
	
	script.Parent.Parent.ResetTime.Text = "Resetting in " .. timeUntilReset .. " seconds..."
	
	
	if timeUntilReset == 0 then
		
		timeUntilReset = 10
	
	
		for i, plr in pairs(game.Players:GetPlayers()) do
			
			KillsODS:SetAsync(plr.UserId, plr.leaderstats.Kills.Value)
		end
		
		for i, leaderboardRank in pairs(script.Parent:GetChildren()) do
			
			if leaderboardRank.ClassName == "Frame" then
				leaderboardRank:Destroy()
			end
		end
		
		
		local success, errorMsg = pcall(function()
			
			local data = KillsODS:GetSortedAsync(false, 50)
			local KillsPage = data:GetCurrentPage()
			
			for rankInLB, dataStored in ipairs(KillsPage) do
				
				
				local name = game.Players:GetNameFromUserIdAsync(tonumber(dataStored.key))
				local Kills = dataStored.value
				
				
				local template = script.Template:Clone()
				
				template.Name = name .. "Leaderboard"
				
				template.PlrName.Text = name
				
				template.Rank.Text = "#" .. rankInLB
				
				template.Kills.Text = Kills
				
				template.Parent = script.Parent				
			end			
		end)
	end
end
--By Gamerpro2p0
1 Like

i think yes char limiiiitttttttttttttttttttttttttt

1 Like

when ever i try it it resets and everything but the number wont change i think its because i use a folder for leaders stats where in this code does it say something about a bool val because i think i need to change it to a folder

1 Like

i cant figure out what broke i tried everything and it still wont work.

1 Like