Need Help With .Touched

Okay, So I got a artillery system and I made the shell (round, bullet whatever you call it) explode on .Touched event. However, im noticing that the .Touched event fires like after 0.5 seconds or something. Basically it fires late. I need alternative’s to .Touched event.

Here’s my code :

—ClonedShell.Touched:Connect(function(hit)
print(hit.Name…" ; exploded")
local Explosion = Instance.new(“Explosion”, workspace)
Explosion.Position = ClonedShell.Position
Explosion.Hit:Connect(function(otherPart)
if otherPart.Parent:FindFirstChild(“Humanoid”) then
otherPart.Parent.Humanoid.Health = 0
print(“killed”)

			ClonedShell.Explosion:Play()
					ClonedShell:Destroy()
						print("done")
				end
			end)
		end)

so when the shell hits something, it bounces off for once and THEN triggers the .Touched event

First devforum post btw so sorry for any mistakes in the topic.

1 Like

:wave:t3: Hello,

This is perhaps because you’re running the Event on the Server, and so the latency of a moving projectile and the Touched event is causing it to seem like it’s late.

If you are doing this Server side, I suggest that you move over to having the Server tell the Clients to replicate the effect on the Client side and handle the Touched there.

Here’s the link to everything you’d need to learn about RemoteEvents to achieve this.

Good luck!

hm okay, but let me tell u the whole concept how this thing works. first there’s a text button which triggers a remote event which a script in serverscriptservice picks up and makes the artillery shoot. so you’re telling me to first fire the remote event which the artillery fires and then again fire a remote event to display the .touched event on all clients?

So what I would do if I was you in this case is the following:

You already fire a RemoteEvent to alert the Server that you’ve clicked the “Trigger” button.

The only thing that the Server should do with that Remote is

FireACannon:FireAllClients()

This Client event should replicate a Projectile and fire it on the Clients with a Touched event locally aswell. (Nothing should be moving on the Server)

so what should i exactly do?
should the fire code be put in a local script or the same server script?

So your Networking will look like this.

Click “Fire” button UI → FireServer → FireAllClients → Create Projectile and fire it locally.

2 Likes

Absolutely works! Thank you SO SO SO SO SO much mate!

1 Like

No worries, maybe mark my answer as correct (If that’s still a thing) to help others !

Have fun coding !

If I read correctly. Then you shouldn’t trust the client for hit detection because it depends on the client. In addition, you shouldn’t use .Touched. It’s okay, but there are better ways.
:+1::+1::+1:

Replicating Projectiles on the Client is absolutely fine and it’s how effects run at a good framerate and react well without having significantly delays.

As he’s using a Projectile, Touched is absolutely fine as well.

You could however take this a step further security wise by implementing Sanity checking on the Server if you want - although I’d argue it’s maybe not required until you see issues pop up as he’s using the Explosion Object rather than Rays.

ok yea, ur right. The touched event does fire much faster now but it still has a very slliiiight delay

it is alot faster than before tho, still a very slight delay

In that case, you could use Ray casting to fire a ray in front of the falling part to detect a part in the path of it and follow out the explosion that way.

I’m currently on phone so I can’t provide the code well for that right now ! But you can definitely find it on the Forums.

2 Likes

u might be also right, i do have a slow internet

1 Like

Of course .Touched is fine just not the best there will always be a slight delay. Therefore, there are other methods like
https://developer.roblox.com/en-us/api-reference/function/BasePart/GetTouchingParts
Also as I said client hit detection is not that good because laggy devices will not register as touched because the projectile could be somewhere else. Even though on your screen it could seem they got hit.

Not here to argue but good job
:+1:

Okay then. i’ll also try raycasting.

1 Like

Why would calling the `:GetTouchingParts’ method and waiting for it to return a table be quicker than listening for an event which fires immediately on Touched ?

No argument of course- I’m just curious :smiley: I usually use Raycasting.

2 Likes

Okay a update, i notice that the shell explode very correctly on things that are solid, like parts! however when i place a blank R6 rig and shoot on it, there’s a slight delay

btw can i use this same method to make shots (bullets) from a actual gun go smoother since when the bullet is released from server, it also has a slight delay before actually starting to move on clients

That’s a good point. You’re right. Thanks.

But what I meant is client detection is not reliable if not secure. Sorry for the misunderstanding.