Expected 'end' (to close 'function' at line 6), got 'else'; did you forget to close 'then' at line 23?

I am trying to make a sword with two slice animations to play on each click but i keep getting the error in the title, i have tried using if instead but it wouldnt make the combo play on each click

this is my script I am quite a noob at scripting so sorry if it is bad

   local db = false
local TrueIfHit = true
local move1 = script.Parent.Move1.Value
local move2 = script.Parent.Move2.Value

script.Parent.Activated:Connect(function(plr)
	if script.Parent.Activated and move1 == true then
		move1 = true
		move2 = false
		print("move1")
		local LoadAnim = script.Parent.Parent.Humanoid:LoadAnimation(script.Parent.Animation)
		LoadAnim:Play()
		script.Parent.Handle.Trail.Enabled = true
		wait(0.4)
		script.Parent.Handle.Trail.Enabled = false
		script.Parent.Handle.Slash:Play()
		TrueIfHit = false

		script.Parent.Handle.Touched:Connect(function(hit)

			if TrueIfHit == false then

				if hit.Parent ~= script.Parent.Parent and hit.Parent:FindFirstChild("Humanoid")then 

					TrueIfHit = true
					script.Parent.Handle.Slice:Play()


					hit.Parent.Humanoid:TakeDamage(15)

				end
			end
		end)
		wait(0.3)
		TrueIfHit = true
		wait(1)
		db = false
		
	end
	else script.Parent.Activated and move1 == true then
	move1 = false
	move2 = true
	print("move2")
	local LoadAnim = script.Parent.Parent.Humanoid:LoadAnimation(script.Parent.Animation2)
	LoadAnim:Play()
	script.Parent.Handle.Slash:Play()
	script.Parent.Handle.Trail.Enabled = true
	wait(0.4)
	script.Parent.Handle.Trail.Enabled = false
	TrueIfHit = false

	script.Parent.Handle.Touched:Connect(function(hit)

		if TrueIfHit == false then

			if hit.Parent ~= script.Parent.Parent and hit.Parent:FindFirstChild("Humanoid") then 

				TrueIfHit = true
				script.Parent.Handle.Slice:Play()


				hit.Parent.Humanoid:TakeDamage(15)

			end
		end
	end)
	wait(0.3)
	TrueIfHit = true
	wait(1)
	db = false
	end)

1 Like

Seems like you’ve got an else in there when all the ifs have been closed above it.

You need to tell your scripts when a certain function and if statement ends, normally you would press enter after doing an if statement or any other stuff like that and it automatically puts the end for you.

Here’s the properly formatted code, this shouldn’t error now:

script.Parent.Activated:Connect(function(plr)
	if script.Parent.Activated and move1 == true then
		move1 = true
		move2 = false
		print("move1")
		local LoadAnim = script.Parent.Parent.Humanoid:LoadAnimation(script.Parent.Animation)
		LoadAnim:Play()
		script.Parent.Handle.Trail.Enabled = true
		wait(0.4)
		script.Parent.Handle.Trail.Enabled = false
		script.Parent.Handle.Slash:Play()
		TrueIfHit = false

		script.Parent.Handle.Touched:Connect(function(hit)

			if TrueIfHit == false then

				if hit.Parent ~= script.Parent.Parent and hit.Parent:FindFirstChild("Humanoid")then 

					TrueIfHit = true
					script.Parent.Handle.Slice:Play()


					hit.Parent.Humanoid:TakeDamage(15)

				end
			end
		end)
		wait(0.3)
		TrueIfHit = true
		wait(1)
		db = false
	elseif script.Parent.Activated then
		move1 = false
		move2 = true
		print("move2")
		local LoadAnim = script.Parent.Parent.Humanoid:LoadAnimation(script.Parent.Animation2)
		LoadAnim:Play()
		script.Parent.Handle.Slash:Play()
		script.Parent.Handle.Trail.Enabled = true
		wait(0.4)
		script.Parent.Handle.Trail.Enabled = false
		TrueIfHit = false

		script.Parent.Handle.Touched:Connect(function(hit)

			if TrueIfHit == false then

				if hit.Parent ~= script.Parent.Parent and hit.Parent:FindFirstChild("Humanoid") then 

					TrueIfHit = true
					script.Parent.Handle.Slice:Play()


					hit.Parent.Humanoid:TakeDamage(15)

				end
			end
		end)
		wait(0.3)
		TrueIfHit = true
		wait(1)
		db = false
	end
end)
1 Like

Thank you it doesnt error now and it also does the two slices, you have been a great help!

After reading more of your code it seems there are some other problems with it

You're checking for events as if they were booleans
script.Parent.Activated:Connect(function(plr) --you have connected the event and this function will be fired whenever the activated event happens
    print("Activated event fired!")
	if script.Parent.Activated then --you are checking for activated even though it's an event and has fired
		
	end
end)

Simply remove the unnecessary checks as you are simply checking if the activated event exists and not if it activated as an event is different than a boolean.

Memory leak/multiple event connection

You are connecting an event multiple times:

script.Parent.Handle.Touched:Connect(function(hit)

	if TrueIfHit == false then

		if hit.Parent ~= script.Parent.Parent and hit.Parent:FindFirstChild("Humanoid")then 

			TrueIfHit = true
			script.Parent.Handle.Slice:Play()


			hit.Parent.Humanoid:TakeDamage(15)

		end
	end
end)

Whenever the activated event happens you connect an event and don’t disconnect it causing the function to be fired multiple times when the event is fired once


I suggest you learn events more in depth, either by using roblox’s documentation/tutorial or other tutorials, like this one by AlvinBlox.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.