onTouch not working for my part

  1. What do you want to achieve? Keep it simple and clear!

I am trying to check if a bullet, that is being moved by tween service, is hitting any object at all

  1. What is the issue? Include screenshots / videos if possible!

The issue is that the script won’t run as intended and doesn’t give me any errors either.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I’ve tried making a ray cast but that won’t work since I am trying to see if the hits anything at its current orientation. I cannot find any other solutions

This is my code for the bullet, I am trying to see if it hits anything at all (other than my tool or myself)
PS: The bullet part starts out in the Server Storage, then is cloned, then the clone’s CFrame is adjusted, then it is shot out with tween service


local bullet = script.Parent

bullet.Touched:Connect(function(hit)
	print(hit.Parent)
end)
1 Like

Try putting the hit function in the same script where you clone it, here’s a quick example:

local bullet = script.Parent:Clone()

bullet.Touched:Connect(function(hit)
	print(hit.Parent)
end)
1 Like
                                               ⚠️ Warning ⚠️

I’m only trying to help

This code was made by AI

It looks like you are trying to use the Touched event to detect if the bullet has hit anything. The Touched event fires when any part of the bullet comes into contact with another part, and it will pass the other part as an argument to the event callback function.

You can use this event to check if the bullet has hit a specific object, or you can use it to check if the bullet has hit any object at all.

To check if the bullet has hit any object at all, you can simply check if the hit argument is nil . If it is nil , then the bullet has not hit any object. If it is not nil , then the bullet has hit an object.

Here is an example of how you could use the Touched event to check if the bullet has hit any object:

bullet.Touched:Connect(function(hit)
  if hit then
    print("Bullet hit an object!")
  else
    print("Bullet did not hit any object.")
  end
end)

I hope this helps! Let me know if you have any other questions.

1 Like

If your bullet moves too fast the .Touched event won’t fire because the collision doesn’t get detected.

1 Like

I highly recommend using the ZonePlus Module for your case.
You’d need to create a zone around the bullet, and then check if an object enters the zone, which means you have a collision. It sounds complicated, but really, its only a few lines of code.
The larger the zone is around the bullet, the more leeway you’ll get, but the less accurate it’ll be.

1 Like

I know how to the touched function works, seen in the code I added. The problem is, my bullets are visually colliding with other parts but nothing is being printed.

I slowed down the tween to 10 seconds but it still doesn’t work. I even shot parts at point blank range. Do you think I should try a different method? The reason I’m checking if the bullets hit is because I’m making a shotgun which shoots out 5 bullets at a slightly random angle.

ZonePlus uses the SpatialQuery API, which is quite different from the Touched event, so it may work better in your case.

I tried this and it still won’t work. Do you think it might be because of the properties of the bullet? CanCollide is on and it is also anchored. Do you have any other ideas? The reason I’m checking if the bullet was touched is because I’m trying to make a shotgun that shoots 5 bullets at a slight random angle, so I don’t think I can use ray casting right now.

Seems useful. It’s 1 A.M. for me right now, so I’ll let you know if it works. Thanks for the link.

1 Like

I’m pretty sure .Touched ignores collisions between anchored parts.

You can use the ZonePlus module or the SpatialQuery API directly like others have suggested, but raycasts would be much more performant.

You’re using TweenService, while the part is anchored? .Touched events do not fire unless it is unanchored touching something, or something unanchored is touch it.

How would I use a ray cast in this case? I don’t know what the parameters should be for the ray.

I tried using their example code:

local Zone = require(game:GetService("ReplicatedStorage").Zone)
local container = script.Parent
local zone = Zone.new(container)

zone.playerEntered:Connect(function(player)
	print(("%s entered the zone!"):format(player.Name))
end)

zone.playerExited:Connect(function(player)
	print(("%s exited the zone!"):format(player.Name))
end)

But it won’t print anything… Is it because both parts are anchored?

Do you have CanQuery on? It’s important to have it on for this to work. Also, CanCollide should be off too.

Yes and Yes. I tested it out a bit and turns out its because my bullet is too short. If the “zone” was tall enough it would work.

Yes, you can just make it taller. Normally I end up having to making tall zones to detect full players.

workspace:Raycast(
    startPos, -- Vector3 where you're shooting from
    direction, -- Vector3 relative to where you're shooting from, with it's length being how far it should check for
    raycastParams -- optional RaycastParams
)

So if you want to raycast from a part 100 studs forwards you would do something like this

-- filter to ignore the part that's shooting the ray
local filter = RaycastParams.new()
filter.FilterType = Enum.RaycastFilterType.Blacklist
filter.FilterDescendantsInstances = {part}
-- or you can just make it non collide and not queryable

workspace:Raycast(
    part.CFrame.Position,
    part.CFrame.LookVector * 100,
    filter
)

If you just want to raycast between 2 points, you can calculate the direction like so

local direction = endPos - startPos

Since you are tweening the bullet I assume that it isn’t moving infinitely fast, so you would need to do multiple raycasts along it’s path. Then it would also be easier to tween the bullet yourself.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.