I am trying to make a weapon that fires balls that has special properties (some bounce twice before expoding, some slide for 5 seconds after collision, some just explode on impact).
The problem is the server detects collision but at the wrong timing. My code is something like this:
bullet = bullet that was shot with velocity property change
bullet.Touched:Wait()
local bulletPosWhenTouched = bullet.Position
I tested this and it seems that the bulletPosWhenTouched is actually being registered as a position prior to the collision most times. Funny how the same code on localscript seems to work. I tried to correct it with raycasting but that way the gravity isn’t accounted on the last seconds, also giving unprecise results.
Edit: I need the exact position of the bullet at the time of collision, not the part that it touched, so I make an explosion at the right position. If my bullet hit the terrain and i get the terrain position it would return the middle of the terrain, which isn’t near the place that the explosion happened most times.
Unfortunately the .Touched event isn’t too accurate when used on the Server.
The two solutions I can come up with is to either:
A. Calculate the hit position on client, by incorporating .Touched in a LocalScript. You will also have to set the Network Owner of the projectile to the player who created it.
Projectile:SetNetworkOwner(player)
B. Use Raycasting to detect the hit point instead of relying solely on .Touched timing.
Here is a post I found that seems to be concerning the same topic:
Yeah, I tried making a raycast from oldPosition and actualPosition, which updates every Stepped:Wait() (oldPosition = actualPosition then actualPosition=bullet.Position).
When the touch happens a ray is created with origin=oldPosition and direction = actualPosition, checking a collision and setting the actual position it collided. But physics isn’t applied on rays so sometimes when a ball hit the border of a slope the ray doesn’t hit that slope.
In this case, since it seems that your map has some complex elements, I would recommend option A. Make sure to check out the post I linked, as the issue and solution was explained nicely.
I think maybe i’ll add a testemony system. The damage is shown just for effect, but every player returns their end positions and the server compares them to see what the most probable result is. It’s a turn based game, so I don’t think it affects the gameplay that much, but it does gets a .2 second delay from knockback and damage being applied. I’ll try your Option A first though.
Edit: apply your option A and use testemony system working just to check hackers, not actually affecting gameplay, but more like an anticheat.