The only part of the code that relates to the animation is what I already posted above:
if animator then
local anim = Instance.new("Animation")
anim.AnimationId = DANCE_ANIM_IDS[math.random(1, #DANCE_ANIM_IDS)]
local track = animator:LoadAnimation(anim)
track.Looped = true
track:Play()
end
So I need to stop the previous animation before loading the new one?
How do I go about tracking a currently playing animation exactly?
Here is the full code for full context:
Summary
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Workspace = game:GetService("Workspace")
local RunService = game:GetService("RunService")
-- ⏱️ Wait for leaderboard data to be populated
local MAX_WAIT = 15
local elapsed = 0
while not ReplicatedStorage:FindFirstChild("LeaderboardData") and elapsed < MAX_WAIT do
task.wait(1)
elapsed += 1
end
local LeaderboardData = ReplicatedStorage:FindFirstChild("LeaderboardData")
if not LeaderboardData then
warn("⚠️ LeaderboardData not found in ReplicatedStorage after waiting.")
return
end
local TopPositionsFolder = Workspace:WaitForChild("Leaderboards"):WaitForChild("TopPositions")
local UPDATE_INTERVAL = 120 -- seconds
local DANCE_ANIM_IDS = {
"rbxassetid://112546761278570", "rbxassetid://92277445768851",
"rbxassetid://78936477286329", "rbxassetid://73580191044993",
"rbxassetid://129245681352728", "rbxassetid://126806545353767",
"rbxassetid://125262177120267", "rbxassetid://120619109566330",
"rbxassetid://110621641901297", "rbxassetid://95734131037432",
"rbxassetid://125237774103542", "rbxassetid://92263601861863",
"rbxassetid://124523990124968", "rbxassetid://124441082067052",
"rbxassetid://80178915788662", "rbxassetid://93886861904250",
"rbxassetid://118190145707853", "rbxassetid://107682172041660",
"rbxassetid://110764344482160", "rbxassetid://74743663547415",
"rbxassetid://108293915786723", "rbxassetid://88485361561231",
"rbxassetid://111727007599426", "rbxassetid://87645436556573",
"rbxassetid://83111406797769", "rbxassetid://72409756539267",
"rbxassetid://127761997509649", "rbxassetid://102624989804510",
"rbxassetid://72969755579148",
}
local STAT_TO_POSITION_NAME = {
Coins = "CoinPosition",
Kills = "KillPosition",
Hatches = "HatchPosition",
Rebirths = "RebirthPosition",
Steals = "StealPosition",
Time = "TimePosition",
}
local function addUIStrokeTo(label)
local stroke = Instance.new("UIStroke")
stroke.ApplyStrokeMode = Enum.ApplyStrokeMode.Contextual
stroke.Thickness = 10
stroke.Parent = label
end
local function attachBillboardToRig(rig, displayName, _)
local existingGui = rig:FindFirstChild("TopBillboard")
if existingGui then existingGui:Destroy() end
local billboard = Instance.new("BillboardGui")
billboard.Name = "TopBillboard"
billboard.Adornee = rig:FindFirstChild("Head") or rig:FindFirstChild("HumanoidRootPart")
billboard.Size = UDim2.new(0, 140, 0, 40)
billboard.StudsOffset = Vector3.new(0, 10, 0)
billboard.AlwaysOnTop = true
billboard.MaxDistance = 150
billboard.Parent = rig
local usernameLabel = Instance.new("TextLabel")
usernameLabel.Size = UDim2.new(1, 0, 0.5, 0)
usernameLabel.BackgroundTransparency = 1
usernameLabel.TextScaled = true
usernameLabel.TextColor3 = Color3.fromRGB(255, 255, 255)
usernameLabel.TextStrokeTransparency = 0.5
usernameLabel.Font = Enum.Font.GothamBold
usernameLabel.Text = displayName
usernameLabel.Parent = billboard
addUIStrokeTo(usernameLabel)
local rankLabel = Instance.new("TextLabel")
rankLabel.Size = UDim2.new(.6, 0, 0.5, 0)
rankLabel.Position = UDim2.new(0, 0, 0.5, 0)
rankLabel.BackgroundTransparency = 1
rankLabel.TextScaled = true
rankLabel.TextColor3 = Color3.fromRGB(255, 200, 50)
rankLabel.TextStrokeTransparency = 0.5
rankLabel.Font = Enum.Font.GothamBold
rankLabel.Text = "RANK #1"
rankLabel.Parent = billboard
addUIStrokeTo(rankLabel)
task.spawn(function()
local hue = 0
while rankLabel and rankLabel.Parent do
hue = (hue + 0.005) % 1
rankLabel.TextColor3 = Color3.fromHSV(hue, 1, 1)
task.wait()
end
end)
end
local function clearAppearanceFromRig(rig)
for _, item in ipairs(rig:GetDescendants()) do
if item:IsA("Accessory") or item:IsA("Shirt") or item:IsA("Pants")
or item:IsA("ShirtGraphic") or item:IsA("BodyColors")
or item:IsA("CharacterMesh") or item:IsA("HumanoidDescription") then
item:Destroy()
end
end
end
local function applyTopPlayerToRig(statName, rig)
if not rig then return end
local statFolder = LeaderboardData:FindFirstChild(statName)
if not statFolder then return end
local topEntry = statFolder:FindFirstChild("1")
if not topEntry then return end
local userId = topEntry:GetAttribute("UserId")
if not userId then return end
local userDesc
local success = pcall(function()
userDesc = Players:GetHumanoidDescriptionFromUserId(userId)
end)
if not success or not userDesc then return end
local displayName = Players:GetNameFromUserIdAsync(userId)
local newRig = Players:CreateHumanoidModelFromUserId(userId)
if not newRig then return end
newRig.Name = rig.Name
newRig.Parent = rig.Parent
newRig:SetPrimaryPartCFrame(rig:GetPrimaryPartCFrame())
rig:Destroy()
-- Optional Scaling
pcall(function()
userDesc.HeadScale = 4.4
userDesc.DepthScale = 4.4
userDesc.WidthScale = 4.4
userDesc.HeightScale = 4.4
userDesc.BodyDepthScale = 4.4
userDesc.BodyHeightScale = 4.4
userDesc.BodyWidthScale = 4.4
end)
local humanoid = newRig:FindFirstChildOfClass("Humanoid")
if humanoid then
pcall(function()
humanoid:ApplyDescription(userDesc)
end)
humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None
humanoid.PlatformStand = true
end
local root = newRig:FindFirstChild("HumanoidRootPart")
if root then
root.Anchored = true
local originalCFrame = root.CFrame
RunService.Heartbeat:Connect(function()
if root and root.Parent then
root.CFrame = originalCFrame
end
end)
end
local animator = humanoid and humanoid:FindFirstChildWhichIsA("Animator")
if not animator and humanoid then
animator = Instance.new("Animator")
animator.Parent = humanoid
end
if animator then
local anim = Instance.new("Animation")
anim.AnimationId = DANCE_ANIM_IDS[math.random(1, #DANCE_ANIM_IDS)]
local track = animator:LoadAnimation(anim)
track.Looped = true
track:Play()
end
attachBillboardToRig(newRig, displayName, statName)
end
local function updateAllRigs()
for statName, rigName in pairs(STAT_TO_POSITION_NAME) do
local rig = TopPositionsFolder:FindFirstChild(rigName)
if rig then
applyTopPlayerToRig(statName, rig)
end
end
end
-- Delay initial update a bit longer to ensure LeaderboardData is populated
task.wait(10)
updateAllRigs()
while true do
task.wait(UPDATE_INTERVAL)
updateAllRigs()
end