Crown adding stops working when a 2nd player joins

  1. Trying to give a crown to a player when they join

  2. It gives the crown to the first player that joins but when a 2nd joins it stops working

  3. I have looked on the Developer Hub have not found anything

local Players = game:GetService("Players")

local function checkWins(player,winNumber)

	if winNumber <= 9 then
		local humanoidRootPart = player.Character:WaitForChild("HumanoidRootPart")
		local crown = game.ServerStorage.Crown1:Clone()
		crown.Parent = game.Workspace
		if crown then
			local root = crown
			if root then
				local rotationSpeed = 1
				local distanceFromPlayer = 3
				local angle = 0

				game:GetService("RunService").Heartbeat:Connect(function(deltaTime)
					if not crown or not root:IsDescendantOf(game) then
						return
					end
					crown.BillboardGui.Frame.Rank.Text = winNumber
					angle = (angle + rotationSpeed * deltaTime) % (2 * math.pi)
					local newPosition = humanoidRootPart.Position + Vector3.new(math.cos(angle) * distanceFromPlayer, 0, math.sin(angle) * distanceFromPlayer)

					root:PivotTo(CFrame.new(newPosition) * CFrame.Angles(0, math.rad(angle * 360 / (2 * math.pi)), 0))
				end)
			else
				warn("Crown does not have a PrimaryPart or HumanoidRootPart.")
			end
		end
	elseif winNumber <= 19 then
		local humanoidRootPart = player.Character:WaitForChild("HumanoidRootPart")
		local crown = game.ServerStorage.Crown2:Clone()
		crown.Parent = game.Workspace
		if crown then
			local root = crown
			if root then
				local rotationSpeed = 1
				local distanceFromPlayer = 3
				local angle = 0

				game:GetService("RunService").Heartbeat:Connect(function(deltaTime)
					if not crown or not root:IsDescendantOf(game) then
						return
					end

					angle = (angle + rotationSpeed * deltaTime) % (2 * math.pi)
					local newPosition = humanoidRootPart.Position + Vector3.new(math.cos(angle) * distanceFromPlayer, 0, math.sin(angle) * distanceFromPlayer)
					crown.BillboardGui.Frame.Rank.Text = winNumber
					root:PivotTo(CFrame.new(newPosition) * CFrame.Angles(0, math.rad(angle * 360 / (2 * math.pi)), 0))
				end)
			else
				warn("Crown does not have a PrimaryPart or HumanoidRootPart.")
			end
		end
		
	elseif winNumber <= 29 then
		local humanoidRootPart = player.Character:WaitForChild("HumanoidRootPart")
		local crown = game.ServerStorage.Crown3:Clone()
		crown.Parent = game.Workspace
		if crown then
			local root = crown
			if root then
				local rotationSpeed = 1
				local distanceFromPlayer = 3
				local angle = 0

				game:GetService("RunService").Heartbeat:Connect(function(deltaTime)
					if not crown or not root:IsDescendantOf(game) then
						return
					end
crown.BillboardGui.Frame.Rank.Text = winNumber
					angle = (angle + rotationSpeed * deltaTime) % (2 * math.pi)
					local newPosition = humanoidRootPart.Position + Vector3.new(math.cos(angle) * distanceFromPlayer, 0, math.sin(angle) * distanceFromPlayer)

					root:PivotTo(CFrame.new(newPosition) * CFrame.Angles(0, math.rad(angle * 360 / (2 * math.pi)), 0))
				end)
			else
				warn("Crown does not have a PrimaryPart or HumanoidRootPart.")
			end
		end
		
	elseif winNumber <= 39 then
		local humanoidRootPart = player.Character:WaitForChild("HumanoidRootPart")
		local crown = game.ServerStorage.Crown4:Clone()
		crown.Parent = game.Workspace
		if crown then
			local root = crown
			if root then
				local rotationSpeed = 1
				local distanceFromPlayer = 3
				local angle = 0

				game:GetService("RunService").Heartbeat:Connect(function(deltaTime)
					if not crown or not root:IsDescendantOf(game) then
						return
					end
					crown.BillboardGui.Frame.Rank.Text = winNumber
					angle = (angle + rotationSpeed * deltaTime) % (2 * math.pi)
					local newPosition = humanoidRootPart.Position + Vector3.new(math.cos(angle) * distanceFromPlayer, 0, math.sin(angle) * distanceFromPlayer)

					root:PivotTo(CFrame.new(newPosition) * CFrame.Angles(0, math.rad(angle * 360 / (2 * math.pi)), 0))
				end)
			else
				warn("Crown does not have a PrimaryPart or HumanoidRootPart.")
			end
		end
		
	elseif winNumber <= 49 then
		local humanoidRootPart = player.Character:WaitForChild("HumanoidRootPart")
		local crown = game.ServerStorage.Crown5:Clone()
		crown.Parent = game.Workspace
		if crown then
			local root = crown
			if root then
				local rotationSpeed = 1
				local distanceFromPlayer = 3
				local angle = 0

				game:GetService("RunService").Heartbeat:Connect(function(deltaTime)
					if not crown or not root:IsDescendantOf(game) then
						return
					end
					crown.BillboardGui.Frame.Rank.Text = winNumber
					angle = (angle + rotationSpeed * deltaTime) % (2 * math.pi)
					local newPosition = humanoidRootPart.Position + Vector3.new(math.cos(angle) * distanceFromPlayer, 0, math.sin(angle) * distanceFromPlayer)

					root:PivotTo(CFrame.new(newPosition) * CFrame.Angles(0, math.rad(angle * 360 / (2 * math.pi)), 0))
				end)
			else
				warn("Crown does not have a PrimaryPart or HumanoidRootPart.")
			end
		end
		
	elseif winNumber <= 74 then
		local humanoidRootPart = player.Character:WaitForChild("HumanoidRootPart")
		local crown = game.ServerStorage.Crown6:Clone()
		crown.Parent = game.Workspace
		if crown then
			local root = crown
			if root then
				local rotationSpeed = 1
				local distanceFromPlayer = 3
				local angle = 0

				game:GetService("RunService").Heartbeat:Connect(function(deltaTime)
					if not crown or not root:IsDescendantOf(game) then
						return
					end
					crown.BillboardGui.Frame.Rank.Text = winNumber
					angle = (angle + rotationSpeed * deltaTime) % (2 * math.pi)
					local newPosition = humanoidRootPart.Position + Vector3.new(math.cos(angle) * distanceFromPlayer, 0, math.sin(angle) * distanceFromPlayer)

					root:PivotTo(CFrame.new(newPosition) * CFrame.Angles(0, math.rad(angle * 360 / (2 * math.pi)), 0))
				end)
			else
				warn("Crown does not have a PrimaryPart or HumanoidRootPart.")
			end
		end
		
	elseif winNumber <= 99 then
		local humanoidRootPart = player.Character:WaitForChild("HumanoidRootPart")
		local crown = game.ServerStorage.Crown7:Clone()
		crown.Parent = game.Workspace
		if crown then
			local root = crown
			if root then
				local rotationSpeed = 1
				local distanceFromPlayer = 3
				local angle = 0

				game:GetService("RunService").Heartbeat:Connect(function(deltaTime)
					if not crown or not root:IsDescendantOf(game) then
						return
					end
					crown.BillboardGui.Frame.Rank.Text = winNumber
					angle = (angle + rotationSpeed * deltaTime) % (2 * math.pi)
					local newPosition = humanoidRootPart.Position + Vector3.new(math.cos(angle) * distanceFromPlayer, 0, math.sin(angle) * distanceFromPlayer)

					root:PivotTo(CFrame.new(newPosition) * CFrame.Angles(0, math.rad(angle * 360 / (2 * math.pi)), 0))
				end)
			else
				warn("Crown does not have a PrimaryPart or HumanoidRootPart.")
			end
		end
		
	elseif winNumber <= 249 then
		local humanoidRootPart = player.Character:WaitForChild("HumanoidRootPart")
		local crown = game.ServerStorage.Crown8:Clone()
		crown.Parent = game.Workspace
		if crown then
			local root = crown
			if root then
				local rotationSpeed = 1
				local distanceFromPlayer = 3
				local angle = 0

				game:GetService("RunService").Heartbeat:Connect(function(deltaTime)
					if not crown or not root:IsDescendantOf(game) then
						return
					end
					crown.BillboardGui.Frame.Rank.Text = winNumber
					angle = (angle + rotationSpeed * deltaTime) % (2 * math.pi)
					local newPosition = humanoidRootPart.Position + Vector3.new(math.cos(angle) * distanceFromPlayer, 0, math.sin(angle) * distanceFromPlayer)

					root:PivotTo(CFrame.new(newPosition) * CFrame.Angles(0, math.rad(angle * 360 / (2 * math.pi)), 0))
				end)
			else
				warn("Crown does not have a PrimaryPart or HumanoidRootPart.")
			end
		end
		
	elseif winNumber <= 499 then
		local humanoidRootPart = player.Character:WaitForChild("HumanoidRootPart")
		local crown = game.ServerStorage.Crown9:Clone()
		crown.Parent = game.Workspace
		if crown then
			local root = crown
			if root then
				local rotationSpeed = 1
				local distanceFromPlayer = 3
				local angle = 0

				game:GetService("RunService").Heartbeat:Connect(function(deltaTime)
					if not crown or not root:IsDescendantOf(game) then
						return
					end
					crown.BillboardGui.Frame.Rank.Text = winNumber
					angle = (angle + rotationSpeed * deltaTime) % (2 * math.pi)
					local newPosition = humanoidRootPart.Position + Vector3.new(math.cos(angle) * distanceFromPlayer, 0, math.sin(angle) * distanceFromPlayer)

					root:PivotTo(CFrame.new(newPosition) * CFrame.Angles(0, math.rad(angle * 360 / (2 * math.pi)), 0))
				end)
			else
				warn("Crown does not have a PrimaryPart or HumanoidRootPart.")
			end
		end
		
	elseif winNumber <= 1000 then
		local humanoidRootPart = player.Character:WaitForChild("HumanoidRootPart")
		local crown = game.ServerStorage.Crown10:Clone()
		crown.Parent = game.Workspace
		if crown then
			local root = crown
			if root then
				local rotationSpeed = 1
				local distanceFromPlayer = 3
				local angle = 0

				game:GetService("RunService").Heartbeat:Connect(function(deltaTime)
					if not crown or not root:IsDescendantOf(game) then
						return
					end
					crown.BillboardGui.Frame.Rank.Text = winNumber
					angle = (angle + rotationSpeed * deltaTime) % (2 * math.pi)
					local newPosition = humanoidRootPart.Position + Vector3.new(math.cos(angle) * distanceFromPlayer, 0, math.sin(angle) * distanceFromPlayer)

					root:PivotTo(CFrame.new(newPosition) * CFrame.Angles(0, math.rad(angle * 360 / (2 * math.pi)), 0))
				end)
			else
				warn("Crown does not have a PrimaryPart or HumanoidRootPart.")
			end
		end
	end
end


local function setupPlayer(player: Player)
	local leaderstats = player:WaitForChild("leaderstats")
	local playerWins = leaderstats:WaitForChild("Wins")

	if (not player.Character) then
		player.CharacterAppearanceLoaded:Wait()
	end

	playerWins.Changed:Connect(function()
		checkWins(player, playerWins.Value)
	end)
end


	for _, player in pairs(Players:GetPlayers()) do
		setupPlayer(player)
	end

Players.PlayerAdded:Connect(setupPlayer)

If anyone knows please help me and thanks!

Is there any reason for this part?

for _, player in pairs(Players:GetPlayers()) do
	setupPlayer(player)
end

If the script is supposed to run when the server starts then I think PlayerAdded connection is enough, but if not it’d be helpful to know.

Check the console for any errors.

i looked a console and did not find any errors

Should i try removing that and give it a test?

Depends, I don’t know your set up so I can’t know if it’s needed, but from what I can see, I assume the script runs as soon as the server starts, meaning there will never be any existing players. This most likely won’t fix anything.

Also when i test it in roblox studio it works but when i test it in roblox it does not work