How do I make player stay in air and turn the face to where mouse is when holding down a button?

Hello. I am currently using a free material to learn more about scripting a combat and I am having problem with this one thing.

Holding:GetPropertyChangedSignal("Value"):Connect(function()
	if Holding.Value ~= "None" then
		HumRP.Anchored = true
		isHolding = true
		if Holding.Value == "Z" then
			Animations.Z:Play()
			Animations.Z:GetMarkerReachedSignal("Idle"):Connect(function()
				if isHolding == true then
					Animations.Z:AdjustSpeed(0)
				end
			end)

			repeat
				HumRP.CFrame = CFrame.lookAt(HumRP.Position, Mouse:GetHit().Position)
				task.wait()
			until Holding.Value ~= "Z"
	else
		HumRP.Anchored = false
	end
end)

As you can see, this script anchors the player when Z button is held down and makes the character turn its face to the mouse location. However this is a local script and is only visible to local player. I tried to turn this into a server script using a RemoteEvent. Here is what I have done:

Local script:

holding:GetPropertyChangedSignal("Value"):Connect(function()
	if holding.Value ~= "None" then
		humRP.Anchored = true
		if holding.Value == "Z" then
			
			repeat
				rotateRemote:FireServer(mouse.Hit.Position)
				task.wait()
			until holding.Value ~= "Z" 
			
		end
	else
		humRP.Anchored = false
	end
end)

Server script:

local RS = game:GetService("ReplicatedStorage")
local remote = RS.Remotes.holdRotateEmote

remote.OnServerEvent:Connect(function(player, mouse)
	
	local char = player.Character
	local HRP = char:FindFirstChild("HumanoidRootPart")
	HRP.CFrame = CFrame.lookAt(HRP.Position, mouse) task.wait()
	
	
end)

When I immediately hold Z right after jumping, the character teleports. Here is a gif to explain it better:
Gyazo

How can I do this better? Thanks in advance.

3 Likes

The reason why your character teleports a bit back is because of client server delay. The server registers your player to be behind what your client sees. This can’t really be 100% fixed on the server’s end. The best the server can do is predict the position.

If I am not mistaken, this effect can be done on the client entirely, since local character CFrame changes replicate across the server.

2 Likes

Really? But last time I checked the rotation was only visible in local screen.
Edit: Yeah its visible only in local screen

1 Like