ElseIf NOT EXECUTING (Inneficient code?)

Hello! I just wanted to execute the following script.
except on the 4th elseif, even with all requirements, it does not work. here, (i put many comments on the error line)
[BTW, NO ERRORS IN OUTPUT]
this script makes 4 m1s.
on the 4th one it does not work

local plr = game.Players.LocalPlayer
local char = plr.Character
local count = 1
local mouse = plr:GetMouse()
local debounce = false
local oldtime 
local newtime

mouse.Button1Down:Connect(function(IS)
	local inactivefolder = char:FindFirstChild("InactiveWeapon")
	local activefolder = char:FindFirstChild("ActiveWeapon")
	
	if IS == true then return end -- check if plr isnt chatting
	if debounce == true then return end 
	if activefolder:GetChildren()[1] then
	if count == 1 then --if m1
		print(count) --print m1
	m1()
	elseif count == 2 then  --if m2
		print(count)  -- print m2
		newtime = os.clock()
			if newtime - oldtime <= 1.5 and newtime - oldtime >= 0.3  then 
			count = count + 1  -- m3
			
			-- slash
			if debounce == true then return end
				debounce = true
				plr.Character.Humanoid.WalkSpeed = 14
				local animation = plr.Character.Humanoid:LoadAnimation(script.M2)
				animation:Play()
				
				--dash
				local dash = Instance.new("BodyVelocity")
				dash.MaxForce = Vector3.new(1,0,1) *30000
				dash.Velocity = char.HumanoidRootPart.CFrame.lookVector * 100
				dash.Parent = char.HumanoidRootPart
				
				game.ReplicatedStorage.Events.Punch2:FireServer()
				task.wait(0.2)
				debounce = false
				for i = 1, 8 do
					task.wait(0.1)
					dash.Velocity *= 0.15
				end
				dash:Destroy()
				
				--oldtime
				oldtime = os.clock()

				
				plr.Character.Humanoid.WalkSpeed = 16
			else 
				count = 1
				m1()
		end
	elseif count == 3 then  --if m2
			print(count)  -- print m2
			newtime = os.clock()
			if newtime - oldtime <= 1.5 and newtime - oldtime >= 0.3  then 
				count = count + 1  -- m3

				-- slash
				if debounce == true then return end
				debounce = true
				plr.Character.Humanoid.WalkSpeed = 14
				local animation = plr.Character.Humanoid:LoadAnimation(script.M3)
				animation:Play()

				--dash
				local dash = Instance.new("BodyVelocity")
				dash.MaxForce = Vector3.new(1,0,1) *30000
				dash.Velocity = char.HumanoidRootPart.CFrame.lookVector * 100
				dash.Parent = char.HumanoidRootPart

				game.ReplicatedStorage.Events.Punch2:FireServer()
				task.wait(0.2)
				debounce = false
				for i = 1, 8 do
					task.wait(0.05)
					dash.Velocity *= 0.15
				end
				dash:Destroy()

				--oldtime
				oldtime = os.clock()


				plr.Character.Humanoid.WalkSpeed = 16
				print(count)
			else 
				count = 1
				m1()
			end
		end
	elseif count == 4 then  --if m4 -- Here's the 'error' even tho there arent any in the output
		print("Number 4")  -- maybe too many elseifs?
		newtime = os.clock() -- the requirements are perfectly met.
		if newtime - oldtime <= 1.5 and newtime - oldtime >= 0.3  then 
			count = 1  -- reset

			-- slash
			if debounce == true then return end
			debounce = true
			plr.Character.Humanoid.WalkSpeed = 14
			local animation = plr.Character.Humanoid:LoadAnimation(script.M4)
			animation:Play()

			--dash
			local dash = Instance.new("BodyVelocity")
			dash.MaxForce = Vector3.new(1,0,1) *30000
			dash.Velocity = char.HumanoidRootPart.CFrame.lookVector * 100
			dash.Parent = char.HumanoidRootPart

			game.ReplicatedStorage.Events.Punch2:FireServer()
			task.wait(0.2)
			debounce = false
			for i = 1, 8 do
				task.wait(0.1)
				dash.Velocity *= 0.15
			end
			dash:Destroy()

			--oldtime
			oldtime = os.clock()


			plr.Character.Humanoid.WalkSpeed = 16
		else 
			count = 1
			m1()
		end
		
	end
end)


function m1()
	spawn(function() 
		task.wait(0.1)
		if count == 1 then
		count = count + 1
		else
			count = 1
		end
				debounce = true
		plr.Character.Humanoid.WalkSpeed = 14
		local animation = plr.Character.Humanoid:LoadAnimation(script.M1)
		oldtime = os.clock()
		animation:Play()
		
		
		local dash = Instance.new("BodyVelocity")
		dash.MaxForce = Vector3.new(1,0,1) *30000
		dash.Velocity = char.HumanoidRootPart.CFrame.lookVector * 100
		dash.Parent = char.HumanoidRootPart
		
		game.ReplicatedStorage.Events.Punch1:FireServer()
		
		debounce = false
		for i = 1, 8 do
			task.wait(0.1)
			dash.Velocity *= 0.15
		end
		dash:Destroy()
		
		oldtime = os.clock()
		
		plr.Character.Humanoid.WalkSpeed = 16
			
		
	end)
end

You have some count = 1 resets in the earlier steps. Are you sure it’s not just hitting one of those?
What are your print(count) statements telling you?

1 Like

On the 3rd hit, i printed again when i already added, so it printed 4
and it’s still not working.
I’m pretty sure the requirements are correct, i could be wrong though


elseif count == 3 then --if m2
print(count) – print m2
newtime = os.clock()
if newtime - oldtime <= 1.5 and newtime - oldtime >= 0.3 then
count = count + 1 – m3

			-- slash
			if debounce == true then return end
			debounce = true
			plr.Character.Humanoid.WalkSpeed = 14
			local animation = plr.Character.Humanoid:LoadAnimation(script.M3)
			animation:Play()

			--dash
			local dash = Instance.new("BodyVelocity")
			dash.MaxForce = Vector3.new(1,0,1) *30000
			dash.Velocity = char.HumanoidRootPart.CFrame.lookVector * 100
			dash.Parent = char.HumanoidRootPart

			game.ReplicatedStorage.Events.Punch2:FireServer()
			task.wait(0.2)
			debounce = false
			for i = 1, 8 do
				task.wait(0.05)
				dash.Velocity *= 0.15
			end
			dash:Destroy()

			--oldtime
			oldtime = os.clock()


			plr.Character.Humanoid.WalkSpeed = 16
			print(count) --here it prints 4, cookie
		else 
			count = 1 
			m1()
		end
	end

Right at the top of your Button1Down:Connect function, before any other checks you should try to print all of the variables used in your checks. It should help you see if all of them are getting updated as expected.
print(count, debounce, newtime, oldtime)

1 Like

Ok, I’m trying that right now
Let me see…
i’ll keep you updated.

Hey, thank you so much.
Now that I solved that, another problem appears… if I just spam mouse button 1 it doubles the damage. even with debounce, this happens. how can i fix it?
(im on mac, idk how to share my video its too heavy)

Is the damage dealt with a server script listening to those Punch1 and Punch2 RemoteEvents?
If so I would would do a similar thing like the previous issue and print out info before each of those FireServer calls.

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