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

Box wont destroy if "h == nil" but why?

local debounce = false

local	Box = Instance.new("Part", Player.Character)
		Box.Size = Vector3.new(3, 5, 3)
		Box.CanCollide = false
		Box.Anchored = false
		Box.Transparency = 1
		Box.Name = "Box"
		
local 	boxweld = Instance.new('Weld')
		boxweld.Name = "CWelds"
		boxweld.Part0 = Player.Character.Torso
		boxweld.Part1 = Box
		boxweld.Parent = Player.Character.Torso
		boxweld.C0 = CFrame.new(0, -1, -2)*CFrame.Angles(0, 0, 0)

wait(0.1)
Box.Touched:connect(function(part)
    local h = part.Parent:FindFirstChild("Humanoid")
	if h and (not debounce) and (h.Parent ~= Player.Character) then
       debounce = true
		 h:TakeDamage(math.random(10, 14)) 
		Box:Destroy()
	
	elseif h == nil then
		wait(0.1)
		Box:Destroy()
	end
end)

This is a piece of code that I took from a larger script I made, everything works fine but on line 27 the "Box" doesn't destroy.

3 comments

1

Box isn't destroying because 'h' still exists.

1

Why would you check for h being == to nil then destroy it. I do not understand this logic; is it only me?

0

No @Obscure it isn't only you but I think he was trying to see if the humanoid no longer exists?

2 answers

Try this at the very end of your script instead

elseif h ~= nil then

Replace "elseif h == nil then" with this code and it should work.

I have no clue why you want to check if humanoid is equal to nil but... try this out.

Box.Touched:connect(function(hit)
    local h = hit.Parent:FindFirstChild("Humanoid")
    if h and (not debounce) and (h.Parent ~= Player.Character) then
       debounce = true
         h:TakeDamage(math.random(10, 14)) 
        Box:Destroy()
if h ~= nil then
        wait(0.1)
        print("Box isn't destroyed")
else
 h = nil then
wait(.1)
Box:Destroy()
    end
end)

I made it to where if the humanoid isnt equal to nil it prints that the box isnt destroyed "else" or otherwise if humanoid is equal to nil then wait(.1) then destroy the box.

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