How should I handle gun raycasts?

How I currently do it
currently the way I handle raycasting for guns is firing multiple rays depending on how many pellets the gun asks for, checking which ones hit, and then sending a table of the ones that hit via a remote event.

on the server it double checks if these hit by firing from the player’s head to the Ray.Position and checking if the Ray.Instance is the same.

Problems
I’ve noticed this sometimes doesn’t work very well though? sometimes shots that should hit don’t, and modifying the direction of the ray to add spread also causes issues.

I’ve seen people say “use FastCast” but it seems a bit much for something basic that I’m trying to do, and even then, I don’t know where I would begin to use it properly.

1 Like

Make sure when checking for a humanoid off of something you hit you check if it exists in both the Parent or the Parent.Parent as hats and gears can cause this to not find a humanoid and return nil.

This may fix your problem of some shots not hitting when they should.

2 Likes

the reason that shots aren’t predictably hitting is probably because of ping and the player’s head position on the server:
when player A shoots and hits player B, a remote is fired
(ping happens)
when the server receives the remote, player B has moved because time has passed
server checks the rays, and since player B has moved, some of the rays are now invalid

you can fix this by having the client send the position of the player they hit
on the server, you should check that the rays hit the player of the given position AND that the sent position is reasonable

DON’T USE FASTCAST

1 Like

I tend to already do this, but thank you for the recommendation!

so I would send what the client hit, and the hit position via an event, and then cast a ray from their head to the part they hit? and then if the magnitude of the hit position and the part’s position is within a reasonable amount, do the damage and stuff?

(sorry if it’s unnecessary rephrases I just want to be sure)

also, if I may ask, is there something wrong with it? would it be good to use in the future for bullet drop and other things? (if I ever actually do those things)

Because IIRC fastcast uses projectiles, which doesn’t work with raycasts. It’s also not too hard to make your own projectile system, and it’s a lot easier to debug when it’s your own code.

1 Like

does anyone know a better way than this? I feel like it’s a bit exploitable : /

client A: sends: {
position of character B,
position of character A(bullet origin),
client A aim direction
}
server: checks: {
is (sent position of character B) reasonable?
is (sent position of character A) reasonable?
does the bullet given by (client A aim direction) and the given positions hit?
}


the reason why any inconsistency exists is because the server would be mixing server state and client state when they are not synced(ping)

inconsistency example:
server: checks: {
is (client A bullet origin) reasonable?
does the bullet given by (aim direction) hit?
}
the server is now checking data from client A against server-side data(assuming character B is perfectly replicated with no delay), which is not what is happening on client A’s screen(delayed by ping)

1 Like

it shouldn’t be more exploitable than any regular raycast bullet system, as everything is magnitude checked with the server so nothing absurd makes it through

of course, regular aimbot and such will still work(nearly impossible to patch), but spoofing player positions to fire through walls won’t be possible

1 Like