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:
server recievs info from the player “I want to throw now”
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.
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.
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.
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?
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.
oh in my post I meant “look” not “loot”
Well there will be a lot grenades
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