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

How do I use the "else" function in scripts?

To be more specific, in this script:

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)
		
		db = false
		
		
		
		end
	end	
end


part.Touched:connect(FindHat)

What I want is if someone is wearing something other than "Silverthornantlers" then it'll run another piece of code. So it's look something like this:

else
myVar.Text = ("You're good to go!")

Help would be greatly appreciated!

3 comments

0

You don't need the () around strings, by the way

0

Please accept my answer.

0

This may not be important but, if, else, and elseif are not functions. They are conditional statements.

3 answers

The Breakdown

There are two different types of else statements. elseif and else. Both being extremely useful when it comes to scripting.

ELSEIF

elseif is used for whenever you need to ask, "Well, if they don't have this, do they have this instead?" An example would be

local a = 5

if a == 6 then --If a = 6 then print A = 6!
	print("A = 6!")
elseif a == 5 then --If A does not = 6, but a = 5, then print A = 5!
	print("A = 5!")
end

The script above would make A equal 5, then ask, "Does A equal 6?", the if statement would return false, in other words, A does not equal 6. With the elseif added it asks another question, "Does a = 5?" The elseif statement would be true, resulting in a print of A = 5! You can have as many elseifs as you want in an if statement.

ELSE

When using an else, it will go ahead and ask the if statement, "Is what you're asking true?" If the if statement returns false, then the code underneath the line of else will run until it hits an end. You can only have one else in an if statement.

An example script would be:

local a = 5
if a == 6 then --If a  = 6 then print, A = 6!
	print('A = 6!')
else --If a does not equal 6, then print, A does not equal 6.
	print('A does not equal 6!')
end

Your script would be:

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

part.Touched:connect(function(hit)
    local hat = hit.Parent:FindFirstChild("SilverthornAntlers")
    local human = hit.Parent:FindFirstChild("Humanoid")
    if human and hat then
        if  db = true then 
            db = false
     		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:TakeDamage(human.MaxHealth)
      		wait(5)
	 	db = true
	elseif db = false
		myVar.Text = "You're good to go!"
		db = true
        end
    end 
end

If you did not understand this, then please leave a comment! If you did, then please accept my answer! =)

2 comments

0

Thanks for explaining everything precisely to me. Although when I inserted the code you added: else myVar.Text = "text" it didn't work. What I want it to do is if I'm not wearing silverthornantlers, then it'll print "you're good to go. Instead what it does is this: If I'm wearing the antlers, it says for half a second "how dare ect." and then it says You're good to go! Help please?

0

updated, try now.

if human and hat then
--Murder
elseif human then
myVar.Text = "You are good to go."
elseif hat then
print("Non-human hat detected.")
else
error("This cannot be reached")
end

Also: It isn't a function, it is a statement.

6 comments

1

I'm confused. Can you elaborate on what your code is? I want to try to understand this and remember it.

0

Well, the if statements condition is checked and if it is true it runs the body and skips to the end, otherwise it checks the next elseif statement and if that checks runs that and skips to the end (this is repeated for all elseifs), and if the original if statements and no elseifs pass it runs the else body.

0

ok, so inserted the code you showed me, but all it does is print "You're good to go" even though I'm wearing the hat. Any suggestions?

0

You wont have set up the if statement correctly. Show me the modified code.

0

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 WRAT

0

TABBING! cringe

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
		
elseif human then
myVar.Text = ("You're good to go!")
elseif hat then
print("Nub")
else
error("This can't")
db = false
		end
	end	
end


part.Touched:connect(FindHat)

2 comments

2

Don't answer your own question (you can edit your question and place the amended script below). Also, the if statement was for the first if statement, not the second (which is a debounce).

0

Ok, thanks for telling me about the editting part. But back to scripting, I'm confused still. What do the if statements have to do with the "elseifs"?

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