I don't get this error, tried closing the loops but still not working

I keep getting the error

12:01:15.042  Workspace.SecondIsland.CoconutTree.ManageTree:33: Expected 'end' (to close 'do' at line 18), got <eof>; did you forget to close 'do' at line 28?  -  Studio  -  ManageTree:33

from this:

local tree = script.Parent
local branches = {tree.LeafBranch1, tree.LeafBranch2, tree.LeafBranch3, tree.LeafBranch4}
local toolToClone = game.ServerStorage.Coconut
local timeOfLife = 0
local timeOfMaxLife = math.random(10,20)

--Defines which branch to spawn on and clones it--
local function spawnFruit()
	--Randomizes branch--
	local leafToSpawn = branches[math.random(1,4)]
	--Clones fruit--
	local clone = toolToClone:Clone()
	clone.Parent = leafToSpawn
	clone.Handle.Position = leafToSpawn.Position
end

--Spawns the fruit at a random interval, and manages tree life--
while true do
	wait(math.random(20,40))
	timeOfLife += 1
	if timeOfLife >= math.floor(timeOfMaxLife/2) then
		for i = 1, #branches do
			branches[i].BrickColor = BrickColor.new("Fawn brown")
		end
		
		spawnFruit()
	else if timeOfLife == timeOfMaxLife then
		for i = 1, #branches do
				branches[i]:Destroy()
		end
	end
		tree.Trunk.Anchored = false
end

Can anyone explaining this for me, and/or give me the fixed code? I’m puzzled on this :confused:

If it wasn’t uncomfortable, could you send the entire code here?

Pretty sure you’re missing an end in your code.

1 Like

did you try to put the end that closes do on the right place
something like this:

	for i = 1, #branches do
		branches[i].BrickColor = BrickColor.new("Fawn brown")
	end

oh it’s not there the problem (i think)

2 Likes

Instead of writing 30 max, just heart the replies as a sign that you’ve read it. “30 max” can get you moderated, so I wouldn’t risk it. Just some feedback

1 Like

Yep, still not working. I’ve counted the amount of needed ends and it doesn’t match up.
EDIT: Wait, I counted 5 conditions and 4 ends, added the last needed one, testing…
IT WORKED! Reply was not exactly the solution, but pointed me to the right place.

1 Like

Have you tried to format the script? Above the script, format selection is written, click on it and right after click on format document. This is an easier way to do what Temeraire149 said.

Oh, you’re just forgetting to put an end under the last end.

1 Like