Scripting mouse follow for player

I’d like to create a function that involves runservice inside my local script for my gun. Basically I want my player to hold his gun and ALWAYS aiming it in the position of where the mouse cursor is.

So if my player has the gun equipped and he holds down RightClick, the player aims the gun in the direction of where the mouse cursor is.

Please help me with creating this script or helping me understand how to create it for my system.

1 Like

Sounds like you’re trying to create a first person weapon. Perhaps the following may be of use?

You can accomplish this with SetPrimaryPartCFrame() and the mouse.Hit property. You can have a bool change to true whenever the mouse has button1down, and false when it isn’t. Then listening to the RunService.Heartbeat event, you can check if the bool is true and if so, set the gun’s CFrame like this:

gun:SetPrimaryPartCFrame(gun.PrimaryPart.CFrame, mouse.Hit.p)

The above code is an extreme oversimplification— you will probably need to do more work to get the gun looking right. But hopefully this will help you get a good start!

1 Like