How could i make a projectile hitbox system properly?

I made a projectile system with linear velocity but after learning it stutters on the server i made it on the client and it has no issues. Now i have a problem with the hitbox i want to make it on the server but i have no idea how to. Any help would be greatly appreciated.

How I did it was with raycasts. On the server you will predict where the client projectile is. Then you will cast raycasts from oldPos to predicted pos every x time. You also should account for ping.

Im so confused what would that achieve?

Hitbox? As the projectile moves you raycast. Sorry for bad explanation.

1 Like

Ohh sorry i didnt understand as well. So could i make the raycast like 2 studs? or would i need some formula like to calculate the change?

I used to have this issue, but my solution is very flawed and prone to security issues.

I would cast a hitbox on the server as well, but when it detects someone, then I would fire a remote to handle everything

Would you make the hitbox also move with the same linear velocity as the projectile?

Well think about it this way. If your projectile moves 10 studs per second. Every second you would raycast from the startPos to the endPos so you would raycast 10 studs. Then the endPos would then become the startPos. 1 second later you would get your endPos. So essentially it’s a loop. Since your projectile is linear you don’t need to much raycasts.

1 Like

Im getting it now. Like you said earlier how would i account for lag?

It moves with the projectile, but not because of the linear velocity.

I use Spatial Query in a RunService and cast it in the same position as the projectile, so every frame there will be a new hitbox until it collides with someone or something.

I’m sorry if this is a little confusing, but I’ll try to explain as best I can if you don’t understand.

1 Like

You can get the ping through player:GetNetworkPing (I’m pretty sure that’s what it’s called). So if you’re moving at 10 studs per second the formula should be something like: speed / (time + ping)
Assuming time is greater than 1. You might have to mess around with the formula since this is what I can think of at the top of my head. You have to test it to make sure it’s correct.

1 Like

This is basically the same thing as the raycast method. However, I only recommend using this way if your projectile is large since this uses more performance.

Also running this every frame will be really bad for performance. You can add a small cooldown and compensate with a larger cast.

A little bit off-topic, but I think the new ShapeCasts would work well as a projectile too as a larger cast.

My method is absolutely flawed in a lot of ways, and I’m willing to look for new ways to improve projectiles.

1 Like

I use spatial query too my old projectile used :GetPartsInParts so i know what your talking about

Can you show me a little bit of an example of how the script would work? Would i put it in a loop? Sorry its kind of my first time using raycasts for hitboxes.

So I’m on mobile so the code might look messy and it’s not a full script.

local hit = false
local oldPos = origin
local cooldown = 0.5

local raycastParams

while not hit do
   task.wait(cooldown)
   local predictedPos
   local raycastResult = workspace:Raycast(oldPos, predictedPos, raycastParams)


      if raycastResult then
         hit = true
      end
      oldPos = predictedPos
end

There might be an error in the code btw. If I get on my computer later I’ll revise it. You also have to communicate with the client when the projectile is destroyed. Also you can have a checkup event to make sure everything is synced.

1 Like