What do you want to achieve?
I want to create a global leaderboard for my game, where the top three players are shown along with their player character models dancing in a separate rig. The leaderboard should update as players progress in the game. The leaderboard are working fine, but for some reason, the character part isn’t.
What is the issue?
I have created two scripts, one for the leaderboard and another to update the player character rigs, but the rigs are not updating even though the leaderboard is changing. I have tried a few solutions, such as looking for solutions on the DevForum, but nothing seems to be working.
What solutions have you tried so far?
I have tried the following solutions:
- Looking for solutions on the DevForum
- Checking if the scripts are named correctly, if there’s any typos, and attached to the right objects
- I also tried reading some docs about CreateHumanoidModelFromDescription
But none of these solutions seem to be working. Here’s the code for the script to update the rigs:
local Players = game:GetService("Players")
local plrName1 = script.Parent["#1"].Nickname.Text
local plrName2 = script.Parent["#2"].Nickname.Text
local plrName3 = script.Parent["#3"].Nickname.Text
local leaderboard = script.Parent.Parent.Parent.Parent
local top1 = leaderboard.Statues.Top1
local top2 = leaderboard.Statues.Top2
local top3 = leaderboard.Statues.Top3
local firstPlayerRig = top1.NumberOne
local secondPlayerRig = top2.NumberTwo
local thirdPlayerRig = top3.NumberThree
-- function to change the rig of a player's character
local function changeRig(player, newRig)
local character = player.Character
if character then
character:Destroy()
end
local newCharacter = Players:CreateHumanoidModelFromDescription(newRig)
newCharacter.Parent = player
end
while true do
task.wait(5)
-- get the appearance model for each player
local player1 = Players:FindFirstChild(plrName1)
local appearanceModel1 = player1 and Players:GetCharacterAppearanceAsync(player1.UserId)
local player2 = Players:FindFirstChild(plrName2)
local appearanceModel2 = player2 and Players:GetCharacterAppearanceAsync(player2.UserId)
local player3 = Players:FindFirstChild(plrName3)
local appearanceModel3 = player3 and Players:GetCharacterAppearanceAsync(player3.UserId)
-- change the rig for each player
if appearanceModel1 then
changeRig(player1, firstPlayerRig)
end
if appearanceModel2 then
changeRig(player2, secondPlayerRig)
end
if appearanceModel3 then
changeRig(player3, thirdPlayerRig)
end
end
And here’s the code for the leaderboard script:
local DataStoreService = game:GetService("DataStoreService")
local GlobalLeaderboard = DataStoreService:GetDataStore("GlobalLeaderboard")
local ScrollingFrame = script.Parent
function UpdateLeaderboard()
local SortedPlayers = {}
-- get all players in the game
for _, player in ipairs(game.Players:GetPlayers()) do
-- get the player's clicks
local Clicks = player.leaderstats.Clicks.Value
-- insert the player's data into the leaderboard table
table.insert(SortedPlayers, {Player = player, Clicks = Clicks})
end
-- sort the leaderboard table by the player's clicks
table.sort(SortedPlayers, function(a, b)
return a.Clicks > b.Clicks
end)
-- update the leaderboard UI
for i = 1, 10 do
local Frame = ScrollingFrame["#" .. i]
local NicknameLabel = Frame.Nickname
-- clear the frame if there's no player data for that rank
if not SortedPlayers[i] then
NicknameLabel.Text = ""
else
-- Update the frame with the player's data
local PlayerData = SortedPlayers[i]
NicknameLabel.Text = PlayerData.Player.Name .. " - " .. PlayerData.Clicks
end
end
end
-- call UpdateLeaderboard every 5 seconds
while true do
UpdateLeaderboard()
wait(5)
end
-- save the leaderboard data to the global datastore when the game is closed
game:BindToClose(function()
local LeaderboardData = {}
-- build the leaderboard data table
for i = 1, 10 do
local Frame = ScrollingFrame["#" .. i]
local NicknameLabel = Frame.Nickname
if NicknameLabel.Text ~= "" then
local PlayerName, Clicks = string.match(NicknameLabel.Text, "^(.-) %- (%d+)$")
LeaderboardData[i] = {PlayerName = PlayerName, Clicks = tonumber(Clicks)}
end
end
-- save the leaderboard data to the global datastore
local success, err = pcall(function()
GlobalLeaderboard:SetAsync("LeaderboardData", LeaderboardData)
end)
if not success then
warn("Error saving leaderboard data: " .. err)
end
end)
-- load the leaderboard data from the global datastore when the game starts
local success, LeaderboardData = pcall(function()
return GlobalLeaderboard:GetAsync("LeaderboardData")
end)
if success and LeaderboardData then
for i = 1, 10 do
local Frame = ScrollingFrame["#" .. i]
local NicknameLabel = Frame.Nickname
-- update the frame with the loaded leaderboard data
if LeaderboardData[i] then
local PlayerData = LeaderboardData[i]
NicknameLabel.Text = PlayerData.PlayerName .. " - " .. PlayerData.Clicks
else
NicknameLabel.Text = ""
end
end
else
warn("Error loading leaderboard data")
end
If anyone has any suggestions or advice on what I should do, I would greatly appreciate it. Also, if you need more details or information, please let me know and I’ll be happy to provide it.
Thank you for your help!