(RESOLVED) Exit door script not working

My code is an on touch script that excludes zombies and doesn’t allow a player to activate the script if there are still zombies in the room. It doesn’t work, and prints ‘h’ but not ‘hi’ or ‘bye’. Here’s my code:

wait(0.2)
local room = require(game.ServerScriptService.Server.Room)
touched = false

function onTouch(hit) -- When it hits a brick.
	if touched == false then
		if script.Parent.Parent:FindFirstChild('Zombie',true) then
			print('hi')
			return end
		else
			print('bye')
			touched = true
			if hit.Parent:FindFirstChild('Humanoid') then
				local p = Instance.new("Explosion") -- Makes an explosion.
				p.Parent = game.Workspace
				p.Position = script.Parent.Position
				p.BlastRadius = 0 -- How big the explosion range is.
				p.BlastPressure = 0 -- How much pressure is in the blast.
				script.Sound:Play()
				game.ReplicatedStorage.nextRoom:Fire()
				script.Parent:Destroy()
			else
				touched = false
				return end
		end
end


script.Parent.Touched:connect(onTouch)
1 Like

maybe try hit:FindFirstAncestor('Zombie') instead of script.Parent.parent:FindFirstChild('Zombie')

1 Like

Oh yeah in the first if statement (findfirstchild) you wrote “return end” instead of return. So the else actually isn’t connected to the if statement that finds the zombies, it’s connected to the if touched == false statement. Since touch is always false the else won’t run.

So just change the return end in the find zombie if statement to a return

so with your suggestion in mind, how can i fix the code?


wait(0.2)
local room = require(game.ServerScriptService.Server.Room)
touched = false

function onTouch(hit) -- When it hits a brick.
	if touched == false then
		if script.Parent.Parent:FindFirstChild('Zombie',true) then
			print('hi')
			return — Changed this 
		else
			print('bye')
			touched = true
			if hit.Parent:FindFirstChild('Humanoid') then
				local p = Instance.new("Explosion") -- Makes an explosion.
				p.Parent = game.Workspace
				p.Position = script.Parent.Position
				p.BlastRadius = 0 -- How big the explosion range is.
				p.BlastPressure = 0 -- How much pressure is in the blast.
				script.Sound:Play()
				game.ReplicatedStorage.nextRoom:Fire()
				script.Parent:Destroy()
			else
				touched = false
				return end
		end
end


script.Parent.Touched:connect(onTouch)

it still doesn’t work and i think its because zombie is a humanoid in an npc in the room.

now it doesn’t print anything at all…

Well that doesn’t make sense because it should run the else statement if it couldn’t find a zombie, even if the hierarchy is wrong

Edit: Change that connect to a “Connect” and remove my comment

i did do it before running the script but it didn’t work

then that means the

script.Parent.Touched:Connect(onTouch)

was never ran (connect and Connect is the same, the capitalization on the C does not matter)

it printed ‘h’ though, so it must have run

There’s no print(‘h’) anywhere on your visible script, would you mind sharing the rest?

oh yeah true i just discovered that

yeah, you’re right, the code isn’t running at all

1 Like

well atleast the touch connect bit

Make sure that the humanoid object is named exactly “Humanoid”, if this script takes any kind of character that you don’t know the humanoid object name of, you can do this instead because there is only one humanoid in a character:-

hit.Parent:FindFirstChildOfClass('Humanoid')

This will retrieve first humanoid object it finds no matter what its name is.

I just solved the issue myself, but I don’t think this will work because I’m only trying to find players, not enemy zombies with a humanoid named ‘Zombie’

You can do this to verify if its a player:

if(game.Players:GetPlayerFromCharacter(<the character you found>) ~= nil) then
    --Will run if its a player
end

ok, ill try to use that in future scripts