How to make this smoother?

So I’m trying to make this hat follow the player however I can’t seem to get it right I feel like it’s always a bit choppy and laggy also it occasionally spazzes out and spins like crazy.

I’m trying to recreate this

This is the code I currently have for making the hat follow the player

	local BodyPos = Instance.new("BodyPosition")

	BodyPos.P = 95000
	BodyPos.Parent = Hat	

	coroutine.resume(coroutine.create(function()

		while wait() do

			if Character:FindFirstChild("Head") then
				
				Hat.Orientation = Vector3.new(0,0,0)
				BodyPos.Position = Character.Head.Position + Vector3.new(0,1.4,0)

			end

		end

	end))	

while wait() do

This causes at least part of of the problem, for 2 reasons:

a: It’s slower than the physics and/or render loop, meaning the hat doesn’t get updated as often as the character moves, meaning it constantly lags behind, then snaps forwards to the correct position. In rapid succession this makes it look “shakey”.

b: It’s out of sync with the render loop, which can also make it look shakey. Check out this reply to a different thread for an explanation of that.

You can probably fix it like this:

--Top of the script:
local RunService = game:GetService("RunService")

...

while true do
    --update the hat
    
    --wait a single frame
    RunService.RenderStepped:Wait()
end
1 Like

It doesn’t have a link to it. I want to know the explanation. :smiley:

1 Like

Hmm so I tried doing this although the hat just stops following the player and stays in the same spot. Not sure if I did it correctly lol.

coroutine.resume(coroutine.create(function()

	while true do

		if Character:FindFirstChild("Head") then

			--Hat.Orientation = Vector3.new(0,0,0)
			BodyPos.Position = Character.Head.Position + Vector3.new(0,1.4,0)

		end

		RunService.RenderStepped:Wait()

	end

end))	

By the way, is this something that should be handled by the client or server

Seems right to me, can’t spot anything that’d make it not move.

You won’t be able to get smooth movement if you handle it entirely on the server. If it’s purely a visual thing, it’s fine to let the client control it.

Yeah the hat just stops following lol

make sure the hat is unanchored and the player has network ownership
then setup and handle the bodyposition on clientside

you might also need to wait a bit because you may have issues if the hat is created before the local scripts are ready

Network Ownership? I have no idea what that is I don’t think I’ve learned about that.
Edit: Wait if I’m doing this on the client then how will I replicate it for other players?

network ownership is where the server gives the client permission to handle the physics on a part
anything physics related the client does to the part will replicate to other players