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)
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)
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
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
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
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
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