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

Why is my script not checking the characters name?

Asked by 5 years ago

I have made a script that will check if the object that touched it is called "NPC1" or "NPC2" and then delete it.

Script:

function onTouch(hit)
    local hum = hit.Parent.Humanoid
            print (hit.Parent.Name)
            if hit.Parent.Name == "NPC1" or "NPC2" then
                hit.Parent:Destroy()
                game.Workspace.Touch.NPCSpawned.Value = false
            end
        end
script.Parent.Touched:Connect(onTouch)

But if I walk my character onto the block, it deletes my character and anyone else. The print I added was to check if the the block is getting the right object touching it and it is. I also previously tried adding a check for humanoid if statement but that didn't work.

1 answer

Log in to vote
0
Answered by
Leamir 3138 Moderation Voter Community Moderator
5 years ago

Hello, narrowricky!

function onTouch(hit)
    local hum = hit.Parent.Humanoid
            print (hit.Parent.Name)
            if hit.Parent.Name == "NPC1" or hit.Parent.Name =="NPC2" then --You have to aways give the condition
                hit.Parent:Destroy()
                game.Workspace.Touch.NPCSpawned.Value = false
            end
        end
script.Parent.Touched:Connect(onTouch)

Good Luck with your games

Ad

Answer this question