Wondering About "Else"

Do I have to add an “else end” after an “if” statement? If so, how do I format it?

My code:

a.Doors.OpenR.MouseButton1Click:connect(function()

	local s1 = Instance.new("Sound")
	s1.SoundId = ("rbxassetid://5774103930")
	s1.Parent = sound1
	s1.Volume = 2

	if script.Parent.Parent.VehicleSeat.Throttle = 0 then
		s1:Play()
		train.Car1.Body.Doors.R["1"].R.Door.CanCollide = false
		train.Car1.Body.Doors.R["1"].L.Door.CanCollide = false
		train.Car1.Body.Doors.R["1"].R.Door.PrismaticConstraint.TargetPosition = -3
		train.Car1.Body.Doors.R["1"].L.Door.PrismaticConstraint.TargetPosition = -3
		train.Car1.Body.Doors.R["1"].R.Door.PrismaticConstraint.Speed = 3
		train.Car1.Body.Doors.R["1"].L.Door.PrismaticConstraint.Speed = 3
		train.Car1.Body.Doors.R["2"].R.Door.CanCollide = false
		train.Car1.Body.Doors.R["2"].L.Door.CanCollide = false
		train.Car1.Body.Doors.R["2"].R.Door.PrismaticConstraint.TargetPosition = -3
		train.Car1.Body.Doors.R["2"].L.Door.PrismaticConstraint.TargetPosition = -3
		train.Car1.Body.Doors.R["2"].R.Door.PrismaticConstraint.Speed = 3
		train.Car1.Body.Doors.R["2"].L.Door.PrismaticConstraint.Speed = 3
		train.Car1.Body.Doors.R["3"].R.Door.CanCollide = false
		train.Car1.Body.Doors.R["3"].L.Door.CanCollide = false
		train.Car1.Body.Doors.R["3"].R.Door.PrismaticConstraint.TargetPosition = -3
		train.Car1.Body.Doors.R["3"].L.Door.PrismaticConstraint.TargetPosition = -3
		train.Car1.Body.Doors.R["3"].R.Door.PrismaticConstraint.Speed = 3
		train.Car1.Body.Doors.R["3"].L.Door.PrismaticConstraint.Speed = 3

		wait(1.3)

		s1:Destroy()
	end
end)

I’ve tried using else end but the text was red, with a syntax error.

1 Like

else needs to be inside the if statement.

3 Likes

To check if something equals something else, use two equal signs (==)
You can put an else after the if, and any code after the else will run if the first piece of code is not run - example:

if 3 > 2 then
    print("3 is greater than 2")
else
    print("3 is not greater than 2")
end

Here, it will print “3 is greater than 2” and will not print “3 is not greater than 2”

if 10 == 7 then
    print("10 = 7")
else
    print("10 does not equal 7")
end

Here, it will print “10 does not equal 7”

If you wanted to write an if-else statement this is how you format it:

local x = false

if (x == false) then -- Check variable with double equals
-- Code here if x is false
else
-- Code here if x is true
end

The else is if the condition is false. So if you check if x is true, and x is false, the else code would run.

Read more here:

1 Like

thanks! and others were talking about elseif, no this is just else end so thank you

1 Like

FYI else end is the exact same as having no else at all

1 Like

You don’t need the else end bit by the way.

if true then
	print("Hello world!")
end

You should only include it if you intend to use it, for example.

if false then
	print("Hello world!")
else
	print("Bye world!")
end