Global leaderboard help

Howdy, so i have global leaderboard working, also the script that morphs dummy into players avatar. How can i connect it so the dummy is the first player in the leadetboard?
The scripts:
Global leaderboard:

local ds = game:GetService("DataStoreService")

local coinsODS = ds:GetOrderedDataStore("CoinsStats")


local timeUntilReset = 10


while wait(1) do
	
	
	timeUntilReset = timeUntilReset - 1
	
	script.Parent.Parent.ResetTime.Text = "Leaderboard refreshing in  " .. timeUntilReset .. " seconds..."
	
	
	if timeUntilReset == 0 then
		
		timeUntilReset = 10
	
	
		for i, plr in pairs(game.Players:GetPlayers()) do
			
			coinsODS:SetAsync(plr.UserId, plr.leaderstats.Time.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 = coinsODS:GetSortedAsync(false, 50)
			local coinsPage = data:GetCurrentPage()
			
			for rankInLB, dataStored in ipairs(coinsPage) do
				
				
				local name = game.Players:GetNameFromUserIdAsync(tonumber(dataStored.key))
				local coins = dataStored.value
				
				
				local template = script.Template:Clone()
				
				template.Name = name .. "Leaderboard"
				
				template.PlrName.Text = name
				
				template.Rank.Text = "#" .. rankInLB
				
				template.Coins.Text = coins
				
				template.Parent = script.Parent				
			end			
		end)
	end
end

and the morph:

local Players = game:GetService("Players")
local Dummy = workspace.Dummy
local ID = --?

local function CreateOfflineCharacter(UserID, Dummy)
	local Appearance = Players:GetHumanoidDescriptionFromUserId(UserID)
	Dummy.Humanoid:ApplyDescription(Appearance)
end

CreateOfflineCharacter(ID, Dummy)
1 Like

From the looks of it, you can have it check rankInLB to see if it’s 1 and change the appearance of the dummy if it is to the person who is in first place

Something like this

local Players = game:GetService("Players")
local ds = game:GetService("DataStoreService")
local coinsODS = ds:GetOrderedDataStore("CoinsStats")

local Dummy = workspace.Dummy

local timeUntilReset = 10

while wait(1) do
	timeUntilReset -= 1
	script.Parent.Parent.ResetTime.Text = "Leaderboard refreshing in  " .. timeUntilReset .. " seconds..."
	
	if timeUntilReset == 0 then
		
		timeUntilReset = 10
	
		for _, plr in pairs(Players:GetPlayers()) do
			coinsODS:SetAsync(plr.UserId, plr.leaderstats.Time.Value)
		end
		
		for _, leaderboardRank in pairs(script.Parent:GetChildren()) do
			if leaderboardRank.ClassName == "Frame" then
				leaderboardRank:Destroy()
			end
		end
		
		
		local success, errorMsg = pcall(function()
			local data = coinsODS:GetSortedAsync(false, 50)
			local coinsPage = data:GetCurrentPage()
		
			for rankInLB, dataStored in ipairs(coinsPage) do
				local userId = tonumber(dataStored.key)
				local name = Players:GetNameFromUserIdAsync(userId)
				local coins = dataStored.value
				
				local template = script.Template:Clone()
				template.Name = name .. "Leaderboard"
				template.PlrName.Text = name
				template.Rank.Text = "#" .. rankInLB
				template.Coins.Text = coins
				template.Parent = script.Parent	

				if rankInLB == 1 then --First place
					local Appearance = Players:GetHumanoidDescriptionFromUserId(userId)
					Dummy.Humanoid:ApplyDescription(Appearance)
				end
			end			
		end)
	end
end

Or if you prefer, put the code outside of the ipairs loop, remove the if statement, and get the first thing in the sorted list

local Appearance = Players:GetHumanoidDescriptionFromUserId(tonumber(coinsPage[1].key))
Dummy.Humanoid:ApplyDescription(Appearance)

Thanks! Also do you know how to fix this? It makes avatar squished: image

Does the humanoid of the dummy have those scale values player models have? If not, maybe add those in?

Or it could be taht the dummy is entirely anchored? Maybe only anchor the HumanoidRootPart of it?

Or you could use CreateHumanoidModelFromUserId to make a model of the player and position it there?

I tried to fix it but it still doesn’t work

local Players = game:GetService("Players")
local ds = game:GetService("DataStoreService")
local coinsODS = ds:GetOrderedDataStore("CoinsStats")

local Dummy = workspace.Dummy

local timeUntilReset = 10

while wait(1) do
	timeUntilReset -= 1
	script.Parent.Parent.ResetTime.Text = "Leaderboard refreshing in  " .. timeUntilReset .. " seconds..."

	if timeUntilReset == 0 then

		timeUntilReset = 10

		for _, plr in pairs(Players:GetPlayers()) do
			coinsODS:SetAsync(plr.UserId, plr.leaderstats.Time.Value)
		end

		for _, leaderboardRank in pairs(script.Parent:GetChildren()) do
			if leaderboardRank.ClassName == "Frame" then
				leaderboardRank:Destroy()
			end
		end


		local success, errorMsg = pcall(function()
			local data = coinsODS:GetSortedAsync(false, 50)
			local coinsPage = data:GetCurrentPage()

			for rankInLB, dataStored in ipairs(coinsPage) do
				local userId = tonumber(dataStored.key)
				local name = Players:GetNameFromUserIdAsync(userId)
				local coins = dataStored.value

				local template = script.Template:Clone()
				template.Name = name .. "Leaderboard"
				template.PlrName.Text = name
				template.Rank.Text = "#" .. rankInLB
				template.Coins.Text = coins
				template.Parent = script.Parent	

				if rankInLB == 1 then --First place
					local NumberOne = Players:CreateHumanoidModelFromUserId(userId)
					NumberOne.Parent = game.Workspace
					local hum = NumberOne:WaitForChild("HumanoidRootPart")
					hum.Position = "66.712, 5, -167.568"
					
				end
			end			
		end)
	end
end

What did i do wrong?

2 things, It’s better to change the CFrame instead of the position in this case. And that’s not how you make a vector 3

hum.CFrame = CFrame.new(66.712,5,-167.568)

Also you have no code to destroy the previous model if the first place user changes

Also every time the leaderboard is refreshing a new model is spawning, how can make it so only one spawns?

That’s hat I had mentioned would happen, Just have a variable set up outside of any event or loop or if statement and use that to store the new model. When it has to make a new model, destroy the old model if the variable has something stored in it and then make the model again

Sorry but i don’t understand im more into UI scripting, im learning in general, can u explain how can i do it?

What you have right now is a variable called NumberOne in the if statement, it’s local to the if statement so the game can’t tell if one had alreayd been made

You just put the NumberOne variable outside near all the other variables

local NumberOne

You set the contents of that instead, then when another model has t obe made, check if there’s something in NumberOne, and if there is, destroy it and then make the model, so in the end, something like this

local Players = game:GetService("Players")
local ds = game:GetService("DataStoreService")
local coinsODS = ds:GetOrderedDataStore("CoinsStats")

local Dummy = workspace.Dummy

local timeUntilReset = 10

local NumberOne

while wait(1) do
	timeUntilReset = timeUntilReset - 1
	script.Parent.Parent.ResetTime.Text = "Leaderboard refreshing in  " .. timeUntilReset .. " seconds..."
	
	if timeUntilReset == 0 then
		
		timeUntilReset = 10
	
		for _, plr in pairs(Players:GetPlayers()) do
			coinsODS:SetAsync(plr.UserId, plr.leaderstats.Time.Value)
		end
		
		for _, leaderboardRank in pairs(script.Parent:GetChildren()) do
			if leaderboardRank.ClassName == "Frame" then
				leaderboardRank:Destroy()
			end
		end
		
		
		local success, errorMsg = pcall(function()
			local data = coinsODS:GetSortedAsync(false, 50)
			local coinsPage = data:GetCurrentPage()
		
			for rankInLB, dataStored in ipairs(coinsPage) do
				local userId = tonumber(dataStored.key)
				local name = Players:GetNameFromUserIdAsync(userId)
				local coins = dataStored.value
				
				local template = script.Template:Clone()
				template.Name = name .. "Leaderboard"
				template.PlrName.Text = name
				template.Rank.Text = "#" .. rankInLB
				template.Coins.Text = coins
				template.Parent = script.Parent	

				if rankInLB == 1 then
					if NumberOne then
						NumberOne:Destroy()
						NumberOne = nil
					end
					NumberOne = Players:CreateHumanoidModelFromUserId(userId)
					NumberOne.Parent = workspace
					local hum = NumberOne:WaitForChild("HumanoidRootPart")
					hum.CFrame = CFrame.new(66.712, 5, -167.568)
				end
			end			
		end)
	end
end

Thanks!! Your so nice! Also im guessing ill need to set the orientation with CFrame?

1 Like

You could if needed, or you can have an invisible part set up so when the model is made, the hUmanoidRootPart’s CFrame is set to that part’s CFrame to make things simpler, that’s what I’ve always done

Alright everything works perfectly, thanks!!

1 Like

Also if i wanted to make TextLabel in SurfaceGui show the best players name, how can i do it? I did it like this but it doesn’t work

NumberOne = Players:CreateHumanoidModelFromUserId(userId)
					NumberOne.Parent = workspace
					local Clone = script.Dance:Clone()
					Clone.Parent = NumberOne
					NumberOne.Name = ""
					local hum = NumberOne:WaitForChild("HumanoidRootPart")
					hum.CFrame = game.Workspace.CFRAME.CFrame
					
					
					local text = game.Workspace.NumberOneName.SurfaceGui.TextLabel.Text
					local BestPlayerName = Players:GetNameFromUserIdAsync(userId)
					text = "Player with the most time: "..BestPlayerName

It’s because you’re changing the contents of a variable, you need to reference the Property directly

local text = game.Workspace.NumberOneName.SurfaceGui.TextLabel
local BestPlayerName = Players:GetNameFromUserIdAsync(userId)
text.Text = "Player with the most time: "..BestPlayerName
1 Like

Okay that’s all, i know its kinda off topic but i sended u request, if you want you can accept it if no, its fine, anyway thank you so much!

1 Like