Touched event not working as intended

  1. What do you want to achieve? Keep it simple and clear!
    I’m trying to insert a script into a part after a form of explosion collides with said part.
  2. What is the issue? Include screenshots / videos if possible!
    A collision is never detected between two parts, but is when the part the explosion collides with is a player.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I wasn’t able to come up with any potential solutions.

I’ll post the area in which I’m having issues at.

script.Parent.Touched:Connect(function(hit) -- It never detects a part it hits unless it's a player character.
	if not hit:FindFirstChild("ColorScript") and script.Parent.CanHurt.Value == true then -- Script I need to insert, and a value that gets enabled when the explosion occurs.
		local scr = script.ColorScript:Clone()
		scr.Parent = hit
		scr.Disabled = false
    end
end)

I have tried printing the hit object whenever a collision should be detected, but it never printed anything in the output unless the object was a child of a character.

Touch event only fires if the part touching it is not anchored. If you need the part to be anchored then just check if the part is in the bounding box of the one your detecting collision of.

I’ve never heard of a bounding box before. How would I work it?

You can use workspace:GetPartBoundsInBox(CFrame, Size, OverlapParams) to get all parts in a box.

The downside to it is that you have to check every single time if it’s inside the box so you would need some sort of loop. There is probably a better way.

I know that workspace:GetPartBoundsInRadius() will detect parts in a sphere, but how would I set the values in the set of parentheses? The part that I was using to check the instances caught in the radius was an explosion using TouchInterest. How would I call the instance to check for parts within said instance?

local Box = workspace.SomePartYouWantAsABox

local Params = OverlapParams.new()
Params.MaxParts = 0 --- Makes MaxParts infinite
Params.FilterType = Enum.RaycastFilterType.Blacklist
Params.CollisionGroup = "Default" 

local Objects = workspace:GetPartBoundsInBox(Box.CFrame, Box.Size, Params) -- this returns an array of objects inside the instance

Now how do I check if a part is within the explosion using an if then statement?

by checking if the object is inside the “Objects” array that gets returned, you can do this multiple ways but i believe you can also use table.find(Objects, some_object) and if it returns an index then you know it’s inside the box

i still dont know what u want but i simplified the script

script.Parent.Touched:Connect(function(hit)
	if not hit:FindFirstChild("ColorScript") and script.Parent.CanHurt.Value then
		local scr = script.ColorScript:Clone()
		scr.Parent = hit
		scr.Disabled = false
    end
end)

also what does script.Parent.CanHurt means? like the part can hurt or? i think u just dont need it because the script is alr working and its just taking fps to progress and it will give u nothing

script.Parent.Touched:Connect(function(hit)
	if not hit:FindFirstChild("ColorScript") then
		local scr = script.ColorScript:Clone()
		scr.Parent = hit
		scr.Disabled = false
    end
end)

and theres the problem, u said it will need to hurt only players but. it will hurt anything if it doesnt have the color script so (i also recommend u to place ur script into serverstorage becuase the script just duplicates and as i said before it eating fps

script.Parent.Touched:Connect(function(hit)
	if not hit:FindFirstChild("ColorScript") and hit:FindFirstChild("Humanoid") then
		local scr = game.ServerStorage.ColorScript:Clone()
		scr.Parent = hit
		scr.Disabled = false
    end
end)

so now i made u fps boost + really easy script and it can hit only players (sorry my english is not good)

How would I write this in a scripting format? Is it possible to use a for _,v in pairs() statement to get all instances within the array?

yes if you do “for _, v in pairs(Objects) do … end”

I seem to be getting this error once the explosion occurs
image

Box is the explosion itself

That’s because you’re using “GetPartBoundsInRadius” and that uses other parameters, you need to put in the radius at the spot where I put “Box.Size” because it’s completely useless. You probably also need to change Box.CFrame to Box.Position because it would only need a Vector3 and not a full CFrame.

I checked the parameters and you indeed need to change Box.Size to your explosion Radius and Box.CFrame to Box.Position

How would I call the Radius of the part?

well, if it’s a sphere then you can just get any of the 3 axis of the size. (Radius = YourSphere.Size.X or Radius = YourSphere.Size.Y or Radius = YourSphere.Size.Z) Because all 3 of them should be the same if it’s a normal sphere.

you have to divide it by 2 though, because else you just have the diameter!

1 Like

After a couple of tweaks, the script works as intended! Thank you so much for helping me out!

1 Like

Great to hear that you got it to work! :grin:

1 Like