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

Expected end to close function near elseif, how to fix this?

Hello,

I'm making a script, once they have been clicked, they have been selected. But the problem is... This script is acting weird. And saying it is missing a end for the function. But I ended it.

This error I got

'end' expected (to close 'function' at line 7) near 'elseif'

Here is the script.

local mouse = game.Players.LocalPlayer:GetMouse()
target1 = nothing
target2 = nothing
target3 = nothing
target4 = nothing
target5 = nothing
mouse.Button1Down:connect(function()
	local part = mouse.Target
	if part.Parent:FindFirstChild("Humanoid") ~= nil then
		print("Humanoid!")
		local humanoid = part.Parent:FindFirstChild("Humanoid") 
		if script.Bools.Target1.Value == false then
			target1 = part.Parent.Name
			script.Bools.Target1.Value = true
			script.Bools.Target1.TargetName.Value = (part.Parent.Name)
			end
		elseif
			script.Bools.Target2.Value == false then
			if part.Parent.Name == target1 then end
			 else
				target2 = part.Parent.Name
				script.Bools.Target2.Value = true
				script.Bools.Target2.TargetName.Value = (part.Parent.Name)
				end
			elseif
				script.Bools.Target3.Value == false then
				if part.Parent.Name == target1 or target2 then end
				else
					target3 = part.Parent.Name
					script.Bools.Target3.Value = true
					script.Bools.Target3.TargetName.Value = (part.Parent.Name)
					end
				elseif
					script.Bools.Target4.Value == false then
					if part.Parent.Name == target1 or target2 or target3 then end
					else
						script.Bools.Target4.Value = true
						script.Bools.Target4.TargetName.Value = (part.Parent.Name)
						end
					elseif
						script.Bools.Target5.Value == false then
						if part.Parent.Name == target1 or target2 or target3 or target4 then end
						else
							target5 = part.Parent.Name
					 		script.Bools.Target5.Value = true
							script.Bools.Target5.TargetName.Value = (part.Parent.Name)
							end
		else
		print("What is this?")
	end
end)

could you help me?

1 answer

✓ Accepted answer

Well, it's most a matter of organization, so you can find the missing ends and fix your if conditions. But I think you should get to know Conditional statements better, because this script is pretty messed up.

I didn't get the purpose of these many conditions, but I organized and fixed your script as I usually do. So, a working code would look like this:

local mouse = game.Players.LocalPlayer:GetMouse()
target1 = nothing
target2 = nothing
target3 = nothing
target4 = nothing
target5 = nothing
mouse.Button1Down:connect(function()
	local part = mouse.Target

	if part.Parent:FindFirstChild("Humanoid") ~= nil then
		print("Humanoid!")
		local humanoid = part.Parent:FindFirstChild("Humanoid") 
	end -- missing end

	if script.Bools.Target1.Value == false then
		target1 = part.Parent.Name
		script.Bools.Target1.Value = true
		script.Bools.Target1.TargetName.Value = (part.Parent.Name)
	elseif -- the elseif is in the if block
		script.Bools.Target2.Value == false then
	end -- missing end

	if part.Parent.Name == target1 then
		-- something here
	elseif
		script.Bools.Target3.Value == false then
	else -- you should put the else last
		target2 = part.Parent.Name
		script.Bools.Target2.Value = true
		script.Bools.Target2.TargetName.Value = (part.Parent.Name)
	end

	if part.Parent.Name == target1 or target2 then
		-- something here
	elseif script.Bools.Target4.Value == false then
		-- something here
	else
		target3 = part.Parent.Name
		script.Bools.Target3.Value = true
		script.Bools.Target3.TargetName.Value = (part.Parent.Name)
	end

	if part.Parent.Name == target1 or target2 or target3 then
		-- something here
	elseif
		script.Bools.Target5.Value == false then
	else
		script.Bools.Target4.Value = true
		script.Bools.Target4.TargetName.Value = (part.Parent.Name)
	end
	
	if part.Parent.Name == target1 or target2 or target3 or target4 then
		-- something here
	else
		target5 = part.Parent.Name
		script.Bools.Target5.Value = true
		script.Bools.Target5.TargetName.Value = (part.Parent.Name)
	end
end)

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