So when I fire a ray on the server, the ray detects the character usually then stuns them for a punch. However if that character were to walk backwards, then the ray won’t detect that player. How do I fix this?
I fire a ray from my players root forwards and add more rays for my hitbox, but when the player moves backwards it doesn’t work at all.
There will always be delay in what the server sees and what the clients see.
More specifically, that’s called ping.
The easiest way to do this would be to instead make the raycast happen locally by the player who is punching, and then fire a remote event to the server telling it who punched who, and then have the server verify positions.
That is unfortunately not a very great way to do this though since the puncher has the clear advantage in that their view of the game is more accurate than their opponents.
The way you currently have it is probably the most fair since both players see the punch behave oddly in an equal fashion.
There has to be a way to make this atleast a bit more efficient though? I mean sure delay can be a problem but there should be ways to detect specific stuff like if the attacker is moving forwards, then make the raycast shoot a longer length? How would I be able to check if the player moves forwards only though instead of just moving in general.