Error with crown and wins adding

You can write your topic however you want, but you need to answer these questions:

  1. Trying to add a crown to a player it works but only for the first player that joins

  2. **


    **

  3. Adding WaitForChild

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)
	local leaderstats = player:WaitForChild("leaderstats")
	local playerWins = leaderstats:WaitForChild("Wins")
	playerWins.Changed:Connect(function()
		checkWins(player, playerWins.Value)
	end)
end


Players.PlayerAdded:Connect(setupPlayer)
while true do
	wait(1)
	for _, player in pairs(Players:GetPlayers()) do
		setupPlayer(player)
	end
end

Anyone know how to fix it please and thanks.

Hello there,

the problem is that the Character of the player has not yet been loaded at the time you try to access it. Meaning Character = nil.

A fix to this is by checking whether the character has been loaded or not.
If not then wait for it to fully load using the CharacterAppearanceLoaded event using it’s :Wait() method.

Your setupPlayer() method is the perfect fit for this implementation.

local function setupPlayer(player: Player)
	local leaderstats = player:WaitForChild("leaderstats")
	local playerWins = leaderstats:WaitForChild("Wins")
	
	-- waiting for the character to fully load
	-- before continueing
	if (not player.Character) then
		player.CharacterAppearanceLoaded:Wait()
	end
	
	playerWins.Changed:Connect(function()
		checkWins(player, playerWins.Value)
	end)
end

Let me know whether this helped you or not.
Feel free to ask questions.

  • DosenSuppe2_0

I tested it in roblox studio and it worked but when i moved over to the roblox it stopped working not sure if that’s a bug or just me

The character of a player has no fixed time when loading.
It depends on your connection. When testing on the studio your character loads very fast as the server is hosted on your machine, however, on a game-server you connect to ROBLOX’s servers thus loading takes longer, in this case it’s long enough to cause this error.

Do you know how to fix that if so can you please send the fix

I already told and explained it to you in my prior post.
Even gave an example of where you can implement it.
You could replace your version of the setupPlayer() method with mine.

I add your version but it still did not work

Any new error? Send a screenshot of the output please.

That’s a different error from a different script called PlayerCollisionOFF and not CrownBehavior.

Thus the bug you’ve reported earlier is fixed.

Please send the code-section of the new error.

But the CrownBehavior is not working it’s not adding my crown like it does in roblox studio

What needs to happen:
image

What happens:

(first image is in roblox studio 2nd is in the game)

1 Like