Stuttery, Glitchy Movement with CFrame.lookAt

I’m working on a script that forces the player (if they have the tool equipped) to temporarily look at the humanoid that they click on. I would also like them to still be able to walk around while looking at the target. Unfortunately, this is the following result:

Code Segment:

aimEvent.OnServerEvent:Connect(function(player, target, aiming, char, hum)
	aiming = true
	
	local targetRoot = target:WaitForChild("HumanoidRootPart")
	local charRoot = char:WaitForChild("HumanoidRootPart")
	
	print("Target:", targetRoot.Parent)
	print("Player:", charRoot.Parent)
	
	local aimCoroutine = coroutine.create(function()
		runService.Heartbeat:Connect(function(step)
			if aiming then
				charRoot.CFrame = CFrame.lookAt(charRoot.Position, targetRoot.position)
			end
		end)
	end)
	
	coroutine.resume(aimCoroutine)
end)

Any help is appreciated

1 Like

From what I can see, I think it’s just because it needs to be faster or something like that. Your character is moving quicker than the function runs. I personally don’t know how to fix that but I assume that is the cause.

1 Like

I also forgot to mention, I would like the player to be able to move while looking at the target.

Try going into the Characters humanoid and setting AutoRotate to false when you are aiming at the other character : D

1 Like

Didn’t seem to do anything. I forgot to mention that I am also trying to allow the player to move while looking at the target

1 Like

What script type is it handled on? Local or Server
On a server side it will always set your characters position to its last pinged position which on heartbeat would not work too well

The script containing the heartbeat function is ServerSide, yes. Do you know of any performant alternatives?

This might help. https://create.roblox.com/docs/reference/engine/classes/Humanoid#AutoRotate

Maybe add a extra script inside the tool:

local tool = script.Parent
local humanoid = tool.Parent:FindFirstChild("Humanoid")
tool.Equipped:Connect(function()
humanoid.AutoRotate = false
end)

tool.Unequipped:Connect(function()
humanoid.AutoRotate = true
end)

Sorry about the old script. I’m just tired right now.

Any character movement should be done on the client side not on the server.

2 Likes

I would, but if I remember correctly, (I likely don’t) other players can’t see client sided character movement. Do you know if there’s any way of doing the movement on the client and still having it be visible to others?

I’ve tried AutoRotate, but it didn’t seem to do much to help

All default avatar controls are client side. They can see it.

1 Like

Probably good idea for Simon make it client then.

1 Like

AutoRotate I think is the only way to keep the character from rotating without using a loop super quickly.

Seems to work great using client only, thanks for the advice and for the future reference.

1 Like

I found a efficient and good way to handle the rotation in this thread if you encounter any performance issues in future

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