How to make crown on top of the head to move smoothly?

Hello, I am trying to make a crown system so when a player has the most amount of money, the player will get a crown on top of his head and It will follow him, It works but If you go a bit faster It slows down, Is there any way to always be on the player head, without some delay? any help is appreciated! :slight_smile:

wait(10)
function abbreviateNumber(n)
	if n > 1000 and n < 1000000 then
		local round = math.floor(n / 1000)
		local round2 = n - round*1000
		return round .. "." .. math.floor(round2/100) .. "K+"
	elseif n > 1000000 and n < 1000000000 then
		local round = math.floor(n / 1000000)
		local round2 = n - round*1000000
		return round .. "." .. math.floor(round2/100000) .. "M+"
	elseif n > 1000000000 and n <  1000000000000 then
		local round = math.floor(n / 1000000000)
		local round2 = n - round*1000000000
		return round .. "." .. math.floor(round2/100000000) .. "B+"
	elseif n > 1000000000000 then
		local round = math.floor(n / 1000000000000)
		local round2 = n - round*1000000000000
		return round .. "." .. math.floor(round2/100000000000) .. "T+"
	else
		return n .. ""
	end
end
function CrownSearch()
	local RichPlayer
	local LastRich = 0
	local Player
	for i,v in pairs(game.ServerStorage.PlayerMoney:GetChildren()) do
		if v.ClassName == "NumberValue" then
			if v.Value > LastRich then
				RichPlayer = v.Name
				Player = game.Players:FindFirstChild(RichPlayer)
				LastRich = v.Value
			end
		end
	end
	if RichPlayer ~= nil and Player~= nil then
		local CashAT = abbreviateNumber(LastRich)
		workspace.Cast.Crown.Value = ("πŸ‘‘ "..RichPlayer.." IS THE RICHEST WITH $"..CashAT.." πŸ‘‘")
		if Player.Character:FindFirstChild("Head") ~= nil then
			if workspace:FindFirstChild("RichestMesh") then
				if workspace.RichestMesh:FindFirstChild("Crown") then
					workspace.RichestMesh.Crown.BodyPosition.Position = Player.Character.Head.Position + Vector3.new(0,3.20,0)
				end
			end
		end
	else
		workspace.Cast.Crown.Value = ("")
	end
end

while wait() do
	if #game.Players:GetChildren() > 0 then
		CrownSearch()
	end
end

8 Likes

Instead of using this method to put it on the player, weld it to the player.

5 Likes

It has a delay is because each line of code is a step, and those steps aren’t noticeable normally but in this case this is an inefficient way to position something with checks going on that have a lot of lines.

3 Likes

How can I weld It with scripts? (sorry I do not know welds well)

4 Likes

First create a new weld, and weld it to the head then crown on creation. Next, send an image of what it looks like in-game without the loop, and I will tell you how to position it correctly

2 Likes

Is It possible to create everything via a script, something like this one?

function abbreviateNumber(n)
	if n > 1000 and n < 1000000 then
		local round = math.floor(n / 1000)
		local round2 = n - round * 1000
		return round .. "." .. math.floor(round2 / 100) .. "K+"
	elseif n > 1000000 and n < 1000000000 then
		local round = math.floor(n / 1000000)
		local round2 = n - round * 1000000
		return round .. "." .. math.floor(round2 / 100000) .. "M+"
	elseif n > 1000000000 and n <  1000000000000 then
		local round = math.floor(n / 1000000000)
		local round2 = n - round * 1000000000
		return round .. "." .. math.floor(round2 / 100000000) .. "B+"
	elseif n > 1000000000000 then
		local round = math.floor(n / 1000000000000)
		local round2 = n - round * 1000000000000
		return round .. "." .. math.floor(round2 / 100000000000) .. "T+"
	else
		return n .. ""
	end
end

function CrownSearch()
	local RichPlayer
	local LastRich = 0
	local Player

	for i, v in pairs(game.ServerStorage.PlayerMoney:GetChildren()) do
		if v.ClassName == "NumberValue" and v.Value > LastRich then
			RichPlayer = v.Name
			Player = game.Players:FindFirstChild(RichPlayer)
			LastRich = v.Value
		end
	end

	if RichPlayer and Player then
		local CashAT = abbreviateNumber(LastRich)
		workspace.Cast.Crown.Value = ("πŸ‘‘ " .. RichPlayer .. " IS THE RICHEST WITH $" .. CashAT .. " πŸ‘‘")

		if Player.Character and Player.Character:FindFirstChild("Head") then
			local head = Player.Character.Head
			local richestMesh = workspace:FindFirstChild("RichestMesh")

			if richestMesh and richestMesh:FindFirstChild("Crown") then
				local crown = richestMesh.Crown

				if not crown:FindFirstChild("Weld") then
					local weld = Instance.new("Weld")
					weld.Part0 = crown
					weld.Part1 = head
					weld.C0 = crown.CFrame:inverse()
					weld.C1 = head.CFrame:inverse()
					weld.Parent = crown
				else
					local weld = crown.Weld
					weld.C0 = crown.CFrame:inverse()
					weld.C1 = head.CFrame:inverse()
				end

				crown.BodyPosition.Position = head.Position + Vector3.new(0, 3.20, 0)
			end
		end
	else
		workspace.Cast.Crown.Value = ("")
	end
end

while wait() do
	if #game.Players:GetChildren() > 0 then
		CrownSearch()
	end
end

3 Likes

Yes, it is possible to create a weld in a script doing this:

local head = head
local crown = crown
local Weld = Instance.new("Weld")
Weld.Part0 = head
Weld.Part1 = crown
Weld.Parent = head
2 Likes

hmm, I did It but for some reason, I am flying. did I do something wrong?

function abbreviateNumber(n)
	if n > 1000 and n < 1000000 then
		local round = math.floor(n / 1000)
		local round2 = n - round * 1000
		return round .. "." .. math.floor(round2 / 100) .. "K+"
	elseif n > 1000000 and n < 1000000000 then
		local round = math.floor(n / 1000000)
		local round2 = n - round * 1000000
		return round .. "." .. math.floor(round2 / 100000) .. "M+"
	elseif n > 1000000000 and n <  1000000000000 then
		local round = math.floor(n / 1000000000)
		local round2 = n - round * 1000000000
		return round .. "." .. math.floor(round2 / 100000000) .. "B+"
	elseif n > 1000000000000 then
		local round = math.floor(n / 1000000000000)
		local round2 = n - round * 1000000000000
		return round .. "." .. math.floor(round2 / 100000000000) .. "T+"
	else
		return n .. ""
	end
end

function CrownSearch()
	local RichPlayer
	local LastRich = 0
	local Player

	for i, v in pairs(game.ServerStorage.PlayerMoney:GetChildren()) do
		if v.ClassName == "NumberValue" and v.Value > LastRich then
			RichPlayer = v.Name
			Player = game.Players:FindFirstChild(RichPlayer)
			LastRich = v.Value
		end
	end

	if RichPlayer and Player then
		local CashAT = abbreviateNumber(LastRich)
		workspace.Cast.Crown.Value = ("πŸ‘‘ " .. RichPlayer .. " IS THE RICHEST WITH $" .. CashAT .. " πŸ‘‘")

		if Player.Character and Player.Character:FindFirstChild("Head") then
			local head = Player.Character.Head
			local richestMesh = workspace:FindFirstChild("RichestMesh")

			if richestMesh and richestMesh:FindFirstChild("Crown") then
				local crown = richestMesh.Crown

				local head = head
				local crown = crown
				local Weld = Instance.new("Weld")
				Weld.Part0 = head
				Weld.Part1 = crown
				Weld.Parent = head
				Weld.C0 = CFrame.new(0, 3, 0)
				--crown.BodyPosition.Position = head.Position + Vector3.new(0, 3.0, 0) 
			end
		end
	else
		workspace.Cast.Crown.Value = ("")
	end
end

while wait() do
	if #game.Players:GetChildren() > 0 then
		CrownSearch()
	end
end

3 Likes

You only need to weld the crown once, no need for a loop.

2 Likes

Also, unanchor the crown. That’s why you’re flying.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.