How do I make a tool point at the player's mouse?

  1. What do you want to achieve? Keep it simple and clear!
    I want to create a script that will have a tool (a flashlight in this case) point at the player’s mouse. I already have a working script that moves a light block, giving a flashlight effect, but I want to make the flashlight move as well.

  2. What is the issue? Include screenshots / videos if possible!
    I tried to have the Handle part CFrame.lookAt() but it’s welded to the player, causing the player to glitch out.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

  • CFrame.lookAt()
  • TweenService:Create({CFrame = CFrame.lookAt())

Is there any way to accomplish this? Any help is appreciated!

1 Like

You need to use lookAt to set the Attachment of a RigidConstraint or some other constraint:

game:GetService("Players").PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		local hrp = char:WaitForChild("HumanoidRootPart")
		
		local p = Instance.new("Part", workspace)
		p.CanCollide = false
		p.Size = Vector3.new(0.5,0.5,3)
		
		local attach1 = Instance.new("Attachment", hrp)
		
		local attach2 = Instance.new("Attachment", p)
		attach2.CFrame = CFrame.new(0,0,1.5)
		
		local cons = Instance.new("RigidConstraint", hrp)
		cons.Attachment0 = attach1
		cons.Attachment1 = attach2
		
		game:GetService("RunService").Stepped:Connect(function()
			attach1.CFrame = hrp.CFrame:Inverse() * CFrame.lookAt(hrp.Position, Vector3.zero) --Look at zero
		end)
		
	end)
end)

This points a part attached to hrp at the origin. You can modify it to instead attach to an arm and point at any position.

1 Like

Try reading about the mouse object on roblox
https://create.roblox.com/docs/reference/engine/classes/Mouse

Then you should tween or CFrame of the tool so it follows the mouse. I think you could also use math.clamp() to clamp the Y-axis so the player doesn’t go into the ground and stuff like that.

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