Player looks at mouse but stays in the same position

Hello, I’m trying to make the player look at where the mouse is pointing. The system works. However, when I try moving, the player will stay in the position the server thinks it’s at. How can I rework this to fix the position problem?

Server script:

	if action == 4 then -- im trying to fit the player looking to the mouse as an action. the parameters are; player, mouseTarget, mousePos
		if mouseTarget then
			character.HumanoidRootPart.CFrame = CFrame.lookAt(
				character.HumanoidRootPart.Position,

				mousePos.Position * Vector3.new(1, 0, 1) + 
				character.HumanoidRootPart.Position * Vector3.new(0, 1, 0)
			)
		end
	end

Localscript:

mouse.Move:Connect(function()
	local hit = mouse.Hit
	
	if mouse.Target then
		remoteFunctions.Action:InvokeServer(4, mouse.Hit, mouse.Target)
	end
end)
1 Like

you don’t rlly need to send the look pos to invoke, lookAt is already replicated to all client since this is changing from the player character, another better way is to use RunService and update the rotation of root part in client instead

here’s a code i have in one of my game (typed on a phone so i cannot confirm if this works)

local playerSV = game:GetService("Player")
local runSV = game:GetService("RunService")

local player_crv = playerSV.LocalPlayer
local char = player_crv.Character
local mouse = player_crv:GetMouse()
local root_par = char.HumanoidRootPart

runSV.RenderStepped:Connect(function()
       local mouse_pos = mouse.Hit.Position
       local look_vect = Vector3.new(mouse_pos.X, root_par.Position.Y, mouse_pos.Z)
       
       root_par.CFrame = CFrame.lookAt(root_par.Position, look_vect)
end) 


4 Likes

Works perfectly! Thanks for the help!

im just trying to test my code reading skills, but does this code rotate the player like it a dungeon crawler?

I don’t play dungeon crawlers, but what it does is that it rotates the player to face the position of the mouse. It wont bend down to look at something, the player will only rotate on the Y axis the whole time.

Here’s a video:

1 Like

yeah that is what i mean, like top down dungeon crawlers do that

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