Error: "Expected 'end' ( to close "do" at line 16), got 'else' ; did you forget to close 'then' at line 17?"

Hi, so i’m trying to script a button prompted door opening script from a local script to the server script via a remote event and I kept getting this ANNOYING error as seen in the title. Here’s the script:

local Player = game.Players.LocalPlayer
local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(Input,Istyping)
   if Istyping then
   	return
   
   elseif Input.KeyCode == Enum.KeyCode.E then
   	DoorOpener:FireServer()
   end
end)

local Insight = false

while wait() do
   if Insight == false then
   	Insight = true
   	local ButtonClone = script.Parent:Clone()
   	ButtonClone.Parent = workspace.CurrentCamera
   	ButtonClone.Adornee = workspace.Door
   	ButtonClone.TextLabel.Visible = true
   end
   else
   if Insight == true then
   	if workspace.CurrentCamera:FindFirstChild("Button")then
   		workspace.CurrentCamera.Button:remove()
   		Insight = false
   		end
   	end
   	
   end
end

The “else” is underlined in red, I’ve been scrutinising the script for about 15 mins now and I can’t identify what went wrong for the life of me.

1 Like

end.
else.
if.

Fix this :wink:

Right now it’s wonky

 	ButtonClone.Adornee = workspace.Door
   	ButtonClone.TextLabel.Visible = true
  elseif Insight == true then
   	if workspace.CurrentCamera:FindFirstChild("Button") then
   		workspace

This is because your if statement already closed by the end before the else

I see, could you also teach me how to understand the error line in the output? It doesn’t make any sense to me no matter how many times i’ve read it.

I’m also having a difficult time identifying which end belongs to which ifs, it’s annoying the hell outta me. How do I identify those correctly?

It’s simple.

The compiler expected keyword “end” and got “else” instead. This means that you messed up the ordering of your words, and put else before the expected end. In your case, your first if Insight == false then creates the conditional, and then you used an end at the end, to close it. But right after, you use an else and an if. Else must follow an if or elseif, so the compiler is confused as there is no if before, as it was closed in the end.

3 Likes

do this

local Player = game.Players.LocalPlayer
local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(Input,Istyping)
   if Istyping then
   	return
   
   elseif Input.KeyCode == Enum.KeyCode.E then
   	DoorOpener:FireServer()
   end
end)

local Insight = false

while wait() do
   if Insight == false then
   	Insight = true
   	local ButtonClone = script.Parent:Clone()
   	ButtonClone.Parent = workspace.CurrentCamera
   	ButtonClone.Adornee = workspace.Door
   	ButtonClone.TextLabel.Visible = true
  
   elseif Insight == true then
   	if workspace.CurrentCamera:FindFirstChild("Button")then
   		workspace.CurrentCamera.Button:remove()
   		Insight = false

   	
   end
end

The thing is that your first if statement is closed so theres nowhere else this ‘else’ belongs to because theres not if statement

Your fixed code will be(hopefully):

while wait() do
      if Insight == false then 
   	   Insight = true
   	   ButtonClone = script.Parent:Clone()
   	   ButtonClone.Parent = workspace.CurrentCamera
   	   ButtonClone.Adornee = workspace.Door
   	   ButtonClone.TextLabel.Visible = true
      elseif Insight == true then -- your else is now changed to elseif because you want two related if statements meaning you should combine it to one whole statement so theres no end here until you're certain the statement is finished
   	    if workspace.CurrentCamera:FindFirstChild("Button")then
   		  workspace.CurrentCamera.Button:Destroy() -- :Remove is deprecated, 
   		  Insight = false
        end
      end -- your done here with the if statement
end

What does the “(to close '“do” at line 16)” and “did you forget to close ‘then’ at line 17” mean? Also I’ve seen "else"s being used directly after “ends” in other scripts and they worked so why did it work in those cases and not this?

An example would be this excerpt from y3llow mustang’s scream script:

function main()
	charging = false 
	local target = findTarget()
	if target then
		if checkSight(target) and math.abs(myTorso.Position.Y - target.Position.Y) < 1.5 or
			checkSight(target) and (myTorso.Position - target.Position).magnitude < 3 then
			local dist = (myTorso.Position - target.Position).magnitude 
			if dist > 3 then
				myHuman.WalkSpeed = 20 
				chase(target)
			end
			
			if dist < 5 then
				meleeAttack(target)
			elseif dist < 50 and dist > 20 then
				rangeAttack(target)
			end
		else
			print("Main function we have to pathfind")
			myHuman.WalkSpeed = 10
			pathToTarget(target)
		end		
	else
		walkRandom()
	end
end

The else didn’t directly follow up with the ifs or elseifs here?

That ‘else’ can sit after the previous ‘end’ because that previous ‘end’ ends a different if statement. The if statement being closed before the final ‘else’ is nested inside the 1st if statement.

I’d suggest reading up on nested control structures.

Ah okay, man how do you even see that, all them if statements are giving me googly eyes just looking at it lol.

It’s just something that comes with time! You’ll get it, just keep practicing, and don’t give up. :wink:

Last question!! I promise lol, What does the “(to close '“do” at line 16)” and “did you forget to close ‘then’ at line 17” mean?

Alright, so the only issue is on line 22, you are closing your if statement with an ‘end’ before you go into your else statement that goes with your if statement. Here’s what should be a working script, I just removed that extra ‘end’ you had.

local Player = game.Players.LocalPlayer
local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(Input,Istyping)
	if Istyping then
		return

	elseif Input.KeyCode == Enum.KeyCode.E then
		DoorOpener:FireServer()
	end
end)

local Insight = false

while wait() do
	if Insight == false then
		Insight = true
		local ButtonClone = script.Parent:Clone()
		ButtonClone.Parent = workspace.CurrentCamera
		ButtonClone.Adornee = workspace.Door
		ButtonClone.TextLabel.Visible = true
	else
	if Insight == true then
		if workspace.CurrentCamera:FindFirstChild("Button")then
			workspace.CurrentCamera.Button:remove()
			Insight = false
		end
	end

end
end
1 Like