.Touched event gets progressivly worse the more its called_

onContext
Ok, so I’m making a naval combat system with projectile-based artillery weapons. And most of the game logic is run serverside, only GUI and inputs happen on the client. Projectiles are lobbed towards targets with velocity in an arc and an event fires when the projectile hits something. Here is a chunk of the code:


I know I should not be using the .Touched event for this because it’s super inconsistent but I did not find any better way for it on the server-side. But suggestions are of course welcome.

The issue
As you see in the video below the hit detection works phenomenally in the beginning but when more shots are fired it gets progressively worse and a delay starts happening and the projectiles start bouncing off. This is no coincidence because the same pattern of behavior happens every single time I test. So that’s why I suspect it to not be the .Touched event being inconsistent but rather that something else is disturbing it. Any suggestions?

Some details

  • No it’s not the projectiles lingering in workspace they get destroyed right away
  • Sounds don’t last over 4 seconds
  • There is an infinite loop on the server like this (is not so CPU heavy, just some calculation)
    image

Thanks for all the help in advance :smiley:

1 Like

I suspect this may be an issue with Network Ownership, which you can read more about here, but I’ll give you the rundown for your scenario.

By default, all unanchored objects are given network ownership, which offloads some of the physics calculations from the server to clients, depending on the distance between the character and the unanchored part. Each player has a radius in which the network ownership of all parts in that radius will be set to the player, thus calculating the physics on the client. This range is not constant. Instead, it starts at a small range when the player joins the game and gradually increases over time until either the maximum size is reached, or the it is determined that the client’s hardware is insufficient.

I believe what is happening here is that initially the network ownership radius of the player is small enough that the projectiles are not in range, but as time progresses the range extends and the projectiles become in range, thus having their network ownership set to the player.

This causes an issue because your touched event is being handled on the server, while the physics is being handled by the client, thus causing latency to be a factor in these calculations. This latency makes the server and all other clients see physics events later than the client that the physics is being calculated. This means that the projectiles are hitting the target and bouncing off before the touched event is being fired on the server causing the explosion.

The potential solution would be to set network ownership of the projectiles to nil immediately after you set their parent to the Workspace.

4 Likes

Thanks alot dude thats exactly what I was looking for :smiley:

Just because you want a curved path doesn’t mean you can’t use raycasting. You just have to approximate the path with many smaller straight segments which you raycast over. That can be a somewhat tricky to set up, but if you continue to have issues in the future it is an option.

2 Likes