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

How do I use else at the end of scripts?

More specifically, everything in this script works, except the else part.

local part = script.Parent
local db = false
local myVar = game.Workspace.Part.SurfaceGui.Test

function FindHat(hit)
	local hat = hit.Parent:FindFirstChild("SilverthornAntlers")
	local human = hit.Parent:FindFirstChild("Humanoid")
	if human and hat then
		if not db then
			db = true
		
		myVar.Text = ("HOW DARE YOU WEAR THAT WRETCHED HAT!?")
		wait(4)
		myVar.Text = ("YOU WILL BE SMITED BY MY WRATH!")
		wait(4)
		myVar.Text = ("DIE!!")
	    wait(2)
		human.MaxHealth = 0
		wait(5)
		myVar.Text = ("  ")
		
		db = false
		
		
		else
			myVar.Text = ("You're good to go")
		
		
		
		end
	end	
end


part.Touched:connect(FindHat)

So, what the script is supposed to do, is detect if you're wearing a certain hat, and if you're not, then it says "you're good to go!". Everything works if you have the hat, but when I DON'T have it, nothing works. Any help please?

1 answer

✓ Accepted answer

I think the reason is that your else statement belongs to the wrong if statement (the inner one rather than the outer one). To fix this, you would simply end the inner if statement before the else:

local part = script.Parent
local db = false
local myVar = game.Workspace.Part.SurfaceGui.Test

function FindHat(hit)
	local hat = hit.Parent:FindFirstChild("SilverthornAntlers")
	local human = hit.Parent:FindFirstChild("Humanoid")
	
	if human and hat then
		if not db then
			db = true

			myVar.Text = ("HOW DARE YOU WEAR THAT WRETCHED HAT!?")
			wait(4)
			myVar.Text = ("YOU WILL BE SMITED BY MY WRATH!")
			wait(4)
			myVar.Text = ("DIE!!")
			wait(2)
			human.MaxHealth = 0
			wait(5)
			myVar.Text = ("  ")

			db = false
		end
	else
		myVar.Text = ("You're good to go")
	end
end

part.Touched:connect(FindHat)

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