Best way to do Raycast Hit detection

How should I do my raycasts for my raycast guns?

Cause I fire the raycasy in the server script instead of local

It’s slower cause it relys on the server, I’m thinking of switching to do the raycast in the local script instead but I’m afraid of hackers with injections firing off 6billion events

2 Likes

you could do both at the same time and then use the server to check how far was the local one from the server calculation

1 Like

What @XDXDXDXDXDXDwwrrw said is the best way to do it

Client detects the input (mouse click for example), do a local raycast for only visuals (muzzle flash, crosshair recoil, hitmarker, etc.) and then fire a RemoteEvent to the server with information about it: Raycast origin and direction (to make sure they are shooting from where they are at), weapon id, ammo count, etc.
You could even add a timestamp / tick count, and then check on the server whether they are shooting way too fast to be possible with whatever gun they have

Server is where you add your per player debounce and sanity checks:
Is the player holding that gun?
Do they have ammo?
Is their location correct?
Is the fire-rate correct?

If they pass all the checks, then you apply the damage and whatnot

2 Likes

It would better to check if the raycast COULD have hit the target rather than replicating the shot exactly. If an object on the server’s check gets in the way, or more likely, out of the way, it could lead to the shot being way off.

1 Like