Mouse.hit not working

I was trying to make a turret rotate towards the players mouse, but it keeps on saying this:
image

This is my code:
image

I’ve gotten rid of the bit that detects if the player is sitting so that i can debug it easier, but nothing is working. Any help?

Is this a server script? Also you should be using RunService over a while loop.

I think you need to check if the player is actually there, so instead of using WaitForChild you should do FindFirstChild and check if the Player actually exists.

It is a serverscript, also i’ll try to switch it to a RunService.

it could be that the mouse isn’t pointing at anything or :GetMouse() hasn’t completely finished.

add a if statement to prevent it from attempting to get the position of nil.
and wait for the mouse before attempting to get the target or the position

You can’t call :GetMouse() on the server. You should be using a client script for this.

1 Like

I’ll try to make it a local script.

I’m trying to make this turret move towards the mouse:
image
This is the explorer for the turret:
image

image

The “Main” part in the “TurretBody” is what rotates.

Please update the title if you want to propose a second issue, I’m pretty sure your original issue is now resolved.

The turret still isent working, it isent outputting any errors either.

Alright let’s see if I can help you.

First, make sure you properly convert the code to client-server communication (for the mouse.hit).
–game.ReplicatedStorage.RemoteEvent:FireServer(mouse.Hit)

turretbase.CFrame = CFrame.new(
turretbase.Position,
mousehit.Position) --this block should update the turretbase's cframe so it faces the mouse hit's position

Is this what you were trying to achieve? By the way, is the rest of the turret welded correctly?

I didn’t know i had to use remote events for the mouse.Hit, thanks! and yes, the turret is welded correctly.

1 Like