How to handle grenade throw

Hi there,
I’m working on a game where throwing explosives is really important. I need it to be visible for every player the same way, the issue is I’m not sure what is going to work out the best. Grenade should explode once it touches a part. The thing is, when it touches a part on server, it might not collide with the same part on client. How should I solve it? I was thinking about:

  1. server recievs info from the player “I want to throw now”
  2. server sends to all of the clients “guys, there is grenade at (x,y,z) with impulse (x,y,z)”
    now grenade spawns in clients only so it should look the same for all players, but the thing is I am not sure how should I detect a collision.

Thanks for your help guys!

You can use raycasting. Since you mentioned that the grenade would explode on contact, set it to non-collide and let it through a wall. At each point/render, you can call a raycast that checks the current location and the previous location it traveled. If the ray cast hits, activate the explosion.

1 Like

I personally turn off the collisions for the projectile and use workspace:GetPartsInPart or workspace:GetPartsInBox. You would have to put the grenades in the OverlapParamaters of course. WorldRoot | Documentation - Roblox Creator Hub.

You could set a .Stepped and check for collisions every frame, or even every few frames, if performance is a concern. RunService | Documentation - Roblox Creator Hub

Make sure to disconnect your .Stepped when it collides with something.

This method is more memory intensive but would be significantly more accurate, especially for a slow moving projectile such as a grenade.

Thanks for reading, I wish you well.

2 Likes

So you suggest me to send info to all clients “grenade thrown at (x,y,z)” and then do calculations for non visible grenade on server, and when it hits on server, what happens? would you recommend to send another event to all clients saying “destroy the grenade now”? Or should I detect collisions also on client and destroy once the collision is detected? The problem is that the grenade leaves a hole in the ground so it might not detect collisions on client properly. Is sending second event to destroy a proper way to do that?

Thank you also a lot! If I could give you both a solution mark :confused:
Thanks for rapid fast help

1 Like

If you want guaranteed consistency, I highly recommend you handle the projectile on the server.

Another thing for significant optimization is making sure that the actual grenade itself is a CFrame variable or a Table, the specific reason why this saves memory is unknown to me.
(if you want it to use physics then you should use a part, but since it explodes upon impact I don’t think you do, You can code bullet drop and gravity yourself, if you really wished to.)-

You could do this like this:

Client sends information to the server >
Server Creates a Table containing all the information for the grenade, and moves it using math, and editing values in the table >
Client invokes the server using RemoteFunctions to retrieve the data of all grenades. If you wished to save more bandwidth, you could have a single remote function that sends back every table. >
Client creates grenade, and moves it by asking for data.

1 Like
  1. please take a loot at my idea and tell my why I can’t just send another event to destroy the part
  2. wouldnt your idea create little stuttering or glitching grenades on client because of the ping?
  1. I’m sorry, I do not understand. I don’t see why this would cause any issues.
  2. You can tween the grenades, to make it smooth. This is fine aslong as you don’t have hundreds of them at once.
1 Like

oh in my post I meant “look” not “loot”
Well there will be a lot grenades :smiley:
I think i ll stick to my idea where server sends info when grenade spawns and the impulse it has and then when it gets destroyed, but thanks a lot for your help! I think in my case Ill use raycasts because of performance but both ideas are amazing and would work well :smiley:

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