Raycasting on client or server side? Which one is better?

Hello. I have created a weapon system that utilizes raycasts for hitting players. It uses a LocalScript to fire a RemoteEvent with important information such as the position of the mouse (Mouse.Hit), and then creates the raycast through a server script which also handles the damage and fires the RemoteEvent back to the client for clientside aesthetic effects like animations and viewmodel particles.

However, I was wondering if it would be better to create the raycast directly through the LocalScript and then fire the RemoteEvent to pass the raycast results to the serverside script in order to handle damage and other things.

Which option would be better (and safer) for a game? Thank you in advance.

3 Likes

you should put the raycast in a server script. if you rely on the client for the position of the raycast, the client could send any position it wants to the server

1 Like

server-sided raycasts will always have an offset because of latency (this is easily seen when you add tracers to those raycasts)

client-sided raycasts will not have offsets, but require verification through the server (which isn’t very difficult to do)

2 Likes

How could I do this ā€œverificationā€? What methods should I use?

  1. have the client send its parameters to the server (preferrably in a table)

  2. have the server recreate the raycast using the given parameters

  3. compare origin distances (get the origin position on the server, store it in a variable, then clientOrigin - actualOrigin.Magnitude)

  4. compare result distances (if (mouseHitPos - recreatedRaycastHitPos).Magnitude > number)

  5. compare raycast lengths (i forgot how to do this lol, but i think you do raycastHitPos - origin and compare it to a number, something like gunRange ← 500)

  6. compare instances that were hit (although i don’t really recommend this as this can be pretty unreliable)

  7. use task.delay to check for teleport or speed cheats (this is entirely optional, simply do serverOrigin - newOrigin.Magnitude)

3 Likes

Very well. I will do that. Thank you.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.