Keep Getting "expected end to close function" Error

Hi Developers! I’m working on my very first game as a beginner at developing. I’m adding script to my rounds (located in a ModuleScript). One problem: I noticed an error saying I needed an “end” to close off my “function at line 105”. I reviewed the specified area of the script error, and found that there actually was an end closing off the function 50 lines (of script) later. I tried rechecking other parts of my ModuleScript but everything seemed to be working fine. Here is the code starting at Line 105 (the error) and ending at Line 177 (where I typed “end”):

function module.StartRound(length,piggy,chapterMap) -- length (in seconds)

local outcome

	game.ServerStorage.GameValues.GameInProgress.Value = true


	for i = length,0,-1 do
		
		if i ==(length - 20) then
			module.TeleportPlayers({piggy},chapterMap.PlayerSpawns:GetChildren())
			status.Value = "Peppa has woken up!"
			wait(2)
		end
		
		local contestants = {}
		
		local isPiggyHere = false
		
		local Escapees = 0
		
		for i, player in pairs(game.Players:GetPlayers()) do
			
			if player:FindFirstChild("Contestant") then
				table.insert(contestants,player)
			elseif player:FindFirstChild("Piggy") then
				isPiggyHere = true
			end
			
		if player:FindFirstChild("Escaped") then
			Escapees = Escapees + 1 
		end
				
		status.Value = toMS(i)
		
		if Escapees > 0 then
			outcome = "escaped"
			break
		end
		
		if not isPiggyHere then -- if isPiggyHere == false / is the piggy dead / left the game?
			outcome = "piggy-left"
			break
		end
		
		if #contestants == 0 then
			outcome = "piggy-killed-everyone"
			break
		end
		
		if i == 0 then
			outcome = "time-up"
			break
		end
		
		wait(1)
	end
		
	if outcome == "piggy-killed-everyone" then
		status.Value = "Peppa has killed everyone."
	elseif outcome == "time-up" then
		status.Value = "Time's Up! Players win!"
	elseif outcome == "piggy-left" then
		status.Value = "Peppa has died! Players win!"		
	elseif outcome == "escaped" then
		status.Value = "Players have escaped! Peppa loses!"
	end		
	
	
	
	wait(5)
					
end

Any suggestions on what might be causing the error to repeat itself even though I fixed it?

I also think that this error is causing return module at the end of the module to be underlined in red. If this theory is incorrect, please do correct me by telling me what is causing the trouble with return module instead.

Any help/criticism is appreciated!

It looks like you’re missing an end at the bottom of your for loop, if I’m not mistaken

3 Likes

Your just missing an end to end a function. Search it up before making a topic

1 Like