Hello! Today I am writing about an issue I have had in Roblox for a little while. My problem is, that, I made a gun (which does not yet shoot bullets, I know how to make it shoot.), but I can’t seem to make it point in the direction of the character’s head. Just so it is clear, I am making an fps game and I am not having a problem with making the gun shoot.
Right so there are 2 options: a simple one and a slightly more involved one.
The simple option uses the Mouse object, more specifically, Mouse.Hit to determine where the user is aiming. You then take the Mouse.Hit and do Workspace:Raycast to create a ray from the aim and find any parts which the ray hit (these would be found using RaycastResult.Instance.)
Then you send a RemoteEvent to the server (remember, never take the client’s word that they have hit something - you must always check). You may or may not wish to fire the bullet on the client merely for the sake of visuals as the server processing may take a splitsecond. The server then checks if you actually have ammo, if you are alive, if you exist and if you have line of sight woth the target (again achieved using Raycasting - draw a ray from the player to the target and see if there is a part in the way. If there is, the hit is invalid). Then you simply deal damage to the client who was hit (using a server-sided script, of course).
So, all in all:
client Raycasts from Mouse.Hit
client fires off a RemoteEvent, passing the information of who it hit
the server evaluates if the hit is valid or not (could be an exploiter)
the server damages the player who was shot
The more involved method does not use Mouse.Hit, but UserInputService to find the X and Y coordinates of the mouse and then ScreenPointToRay to convert these into a ray. You can take infotmation from this ray to do Raycasting (2 distinct things). From here, all is the same.
Hello! I appreciate the help, but that is not what I asked. I asked if there was a way to point/aim the gun; a bit like the guns in arsenal. As noted in my description, I know how to code the bullet, but I want the gun to follow where the head is looking.
Maybe you could use Mouse.Hit and Hit.p to change the AR’s rotation towards the object, or maybe weld the weapon to the Head if the Head already moves.
The head does not already move, but you gave me an idea; what if I made the HumanoidRootPart move?
The only problem is making the Torso move. If anyone knows how to do it, please tell me
Wouldn’t that make the whole player rotate ? I saw a tutorial on how to make an
NPCs head follow a player, you could try stitching some code together to make
your AR aim somewhere.
It’s okay, I also think you should use the Neck’s Motor6D to rotate the head. EDIT : Sorry, I thought it was a tutorial at first, I apologize if I confused you more.
Using that kind of program will only rotate the head not the torso, and since a fps game is a “first person” game, it automatically turns the head when looking right to left, the problem is up and down.