How do I better optimize my gun script? (RayCasts)

I have seen countless threads of people saying to do the ray casts on the server, then some on the client.

I’m not sure how I can optimize it so there is little to no delay between the server, because not everyone has fast internet.

This is what it currently looks like:

Client: (sends the remote to the server that has all the ray cast parameter stuff)

Server: (processes and handles the ray cast, which also handles the sound, effects, etc)

Any feedback would be good!

1 Like

Handle the calculation on the local client, then send a remote events to server IF you need to affect something on server. All the visual thingy should be on client and sent the server then fireallclient and run on other player client so its also saw by other player client without touching the server.

I think minimized action in server will reduce lag, my client script is over 300 line but my server is only about 30.

1 Like

i think the casting and processing should be done on the client, and then tell the server to tell all the other clients to cast a cosmetic projectile so other clients can view it. this makes things a bit more complicated but casting it on the server would cause it to look a bit choppy especially if your connection isnt stable and it doesnt replicate consistently

1 Like

You should always think of what would happen if the client sent corrupt data (wrong type) or incorrect/exploited values. With that script, you’re allowing a possible hacker to change pretty much everything about the bullet/raycast and that’s no good.

You can start by removing the GunTool.PrimaryPart.Position and retrieve that value from the server. This way, the player can’t change the origin of the bullet, preventing a hack that would just constantly send a bunch of events to create rays that shoot every player anywhere on the map.

If your game is locked in first-person, you can also replace the ClientMouse.Hit.Position with the camera rotation, making it so the only value that the client can change is the direction they’re looking.

As for the latency, there’s not much you can do - things like a player deaths should be handled by the server and automatically replicated by the Roblox engine.

Visual effects, however, should be done only on the client, since a lot of effects involve instancing and managing many parts.
For example, a bullet trail could be implemented by having the server send a network event to all players, each time a bullet is shot. When the client receives this event, it can read the values and display the trail. There would be a bit of latency for your own bullet trails, but this can be fixed by having the client immediately rendering a trail right after it sends GunAction / Shoot and making it so the server sends the trail to every player except the one that fired the shot.

Remember to also typecheck all values received from the client, otherwise the function can error and break even more things.

Good luck!

1 Like

Preciate it!

Not to worried about exploiters at the moment because I’m aware that the new anti-cheat allows for far less exploiters than before. Since my game is in its early stage, I’ll probably implement something in the future.

1 Like

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