Issues with getting part name from workspace. Please help

I can’t believe I ran into another simple issue on my very simple script.
I wanted to make a part be able to disappear after a particle effect to give it the glass break effect when it collides with a bullet in-game. This is what I have so far.

function onTouch(hit)
** if hit.Name == “Bullet” then **
** script.Parent.Transparency=1**
** script.Parent.CanCollide=false**


end
end

script.Parent.Touched:connect(onTouch)

My issue is the hit-name. The bullets in the debris folder are named after the creator with ‘bullets’ after it. For example, a bullet I created would be named PROTO_TECH_bullet. This is my problem. The script will not fire if the hit function only functions when it looks for a part named Bullet, because everyone’s username will be different and that will change the name of the bullet part in the debris folder. Anybody have a solution? (I can’t believe I’m asking help for this.)

Instead of an exact match you can use string.match to determine if the name contains _bullet. If the string contains the specified pattern it returns it otherwise returns nil so this should work

function onTouch(hit)
  if string.match(hit.Name, "_bullet") then
    script.Parent.Transparency = 1
    script.Parent.CanCollide = false
  end
end

script.Parent.Touched(onTouch)

In future it would be helpful if you could format code using the 6th button in the editor toolbar as I have.

2 Likes

A more effective system, in my opinion, would be tagging all bullets using CollectionService, and checking for that tag on your part

1 Like

string.match is a new thing for me, so this might come in handy for future projects. Thanks for the feedback! And yes, I didn’t know how to format my code, so I appreciate the help.