Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is my script not checking the characters name?

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

✓ Accepted answer

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

This discussion is preserved as it was. New answers, comments, and votes are closed.