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)
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
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)
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’