Problem with CFrame.lookAt()

[REPOST]

Hello!
I need help for with my code.
In my code, I anchor the HumanoidRootPart, and after anchoring it, I see its CFrame with CFrame.lookAt(), but as you can see (in the video below), it seems to “roll back” and move the character to older position. I really don’t understand why it does that.
Since it fires a high amount of events for that, I tried to just fire 1 event, but it did not solve the problem!

Here are the important piece of code:

Client Side:

UIS.InputBegan:Connect(function(input, gpe)
	if gpe then return end
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.E then
			if UIS:IsKeyDown(Enum.KeyCode.E) then
				local charge = 0
				repeat 			
					local mousepos = game.Players.LocalPlayer:GetMouse().Hit.p
					if charge < 5 then
						charge += .05
					end
					chargingEvent:FireServer(charge, "Water", "WaterBall", mousepos)
					task.wait()		
				until UIS:IsKeyDown(Enum.KeyCode.E) and UIS:IsKeyDown(Enum.KeyCode.One) or input.UserInputState == Enum.UserInputState.End
			end
		end
	end
end)

Server Side:

charge.OnServerEvent:Connect(function(plr, charge, art, attack, mousepos)
	if art == "Water" then
		if attack == "WaterBall" then		
			charging = true				
			print("Received")
			local name = plr.Name.."Folder"
			local sphere = rp.Abilities.Water.Ball		
			if not workspace:WaitForChild(name):FindFirstChild("Ball") then			
				local chargeSphere = sphere:Clone()		
				if workspace:FindFirstChild(name) then
					if chargeSphere then
						chargeSphere.Parent = workspace:FindFirstChild(name)
						chargeSphere.Transparency = .5
					end
				end
			end	

			local chargeSphere = workspace:FindFirstChild(name):WaitForChild("Ball")
			local hrp = plr.Character:FindFirstChild("HumanoidRootPart")
			hrp.Anchored = true
			hrp.CFrame = CFrame.lookAt(hrp.Position, mousepos, Vector3.new(0,1,0))
			chargeSphere.Size = Vector3.new(charge+1, charge+1, charge+1)
			chargeSphere.CFrame = hrp.CFrame * CFrame.new(0,0,-5)
			chargeSphere.Anchored = true
			wait()		
		end
	end
end)

My impression is that this issue is being caused because the server doesn’t see the character where the client does. As I’m sure you’re aware, your client handles the movement for your character. But it takes time for your client to communicate to the server where you’re moving. So when you click and it sends the event to the server, it anchors your HRP and changes the CFrame based on where the server sees that its position is.

A recommended solution I could propose would be for the client to send the HRP’s position through the RemoteEvent so that the server could use that instead with no interpolation occurring.

Scratch that because it would cause the exact same issue. The best way I can think of to approach this is to have the client handle anchoring and orienting the HRP to avoid any networking delay entirely.

1 Like

If this were the solution then couldn’t HRP.CFrame be connected to heartbeat and have it constantly update that way instead?

1 Like

This should be the only way since clients has full ownership of their character anyways. After anchoring, you can then send the signal with the cast origin.

1 Like

That what I thought, there is delay between client and server, but if I anchor and orient from client, other people won’t see it, am I wrong? And if i’m right, then this is not the solution i’m searching for, but thank you for your answer, it helps a lot !

the first solution you sent (sending hrp pos throught event) is great, it reduces the delay by alot, there is still a small delay, but not too big, I might keep that, because setting CFrame from client bring more problem after, because I need other people to see what player is doing. thank you for your help !

I’m glad it worked out for you. However, as @OniiSamaUwU stated, the client has full ownership of their character. That means that you can manipulate the character’s position however you want in a localscript and it will be replicated. Furthermore, you shouldn’t experience any of the delay you were experiencing at all.

1 Like

Sorry to bother you again, but I have one last question, CFrame.lookAt() doesn’t replicate from client to server, maybe I did not understand something but, I did manipulate anchoring and CFraming from client, but except anchoring, CFraming does not replicate, am I missing something?

I’m not sure why that’d be happening. If you see something with your character happen on your client, the server should see it as well. Perhaps try setting Humanoid.AutoRotate = false before you set the CFrame, then back to true before you un-anchor it.

1 Like

Ill try that thank you for helping again!

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