Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do I get the name of a brick that's hit another brick?

Asked by
Nidoxs 190
9 years ago

So I have a gun that fires a bullet, the bullets are called "Bullet" when I fire a bullet at the trigger brick I wan't it to print I got shot, and else to do nothing if it didn't get shot by a part called bullet. Here's what I have but It won't work! Much appreciated if you could help me! :)

a = script.Parent
function onBrickShot(hit)
    if hit.Parent.Name == "Bullet" then
        print("I am a brick that has just been shot!")
        else
    end
end
script.Parent.Touched:connect(onBrickShot)
0
It may not work if bullet is anchored. ChemicalHex 979 — 9y

1 answer

Log in to vote
2
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

hit is equal to the Part that fired the event. If that part is a bullet, then hit will equal that bullet. Therefore you need to check if hit.Name equals "Bullet". hit.Parent.Name will just give you the name of its Parent, which is most likely Workspace.

 --You never use the variable defined on this line
function onBrickShot(hit)
    if hit.Name == "Bullet" then
        print("I am a brick that has just been shot!")
        --No need for the else
    end
end

script.Parent.Touched:connect(onBrickShot)
0
Thanks Perci! I really appreciate it! Nidoxs 190 — 9y
Ad

Answer this question