Help resolving script error (probably easy)

so, i made a script combat system and its having a weird bug.

heres how the script should work:
click
punch1
click
punch2
click
punch3
click
punch4

heres how it works instead:

click
punch1
punch2
punch3
punch4

btw, if you dont punch within 1.5 seconds after the last punch, it will go back to the first punch, and resets after punch 4 as well.

here is the script:

local UIS = game:GetService("UserInputService")
local attacknum = 0
local Mscript = script.Parent.testimgm1m
local debounce = false
local newtick = nil
local ticktime = nil

UIS.InputBegan:Connect(function(input, istyping)
if input.UserInputType == Enum.UserInputType.MouseButton1 and not istyping and debounce == false then
	--first punch
	if attacknum == 0 then
		debounce = true
		require(Mscript).punch1()
		attacknum = 1
		wait(0.5)
		debounce = false
		ticktime = tick()
	end
	--timeout punch 2
	elseif attacknum == 1 then
		newtick = tick()
		if newtick - ticktime > 1.5 then
		debounce = true
		require(Mscript).punch1()
		attacknum = 0
		wait(0.5)
		debounce = false
		ticktime = tick()
		end
	end
	--timed punch 2
	if attacknum == 1 then
		newtick = tick()
		if newtick - ticktime <= 1.5 then
		debounce = true
		require(Mscript).punch2()
		attacknum = 2
		wait(0.5)
		debounce = false
		ticktime = tick()
		
	end
	--timeout punch 3
	elseif attacknum == 2 then
		newtick = tick()
		if newtick - ticktime > 1.5 then
		debounce = true
		require(Mscript).punch1()
		attacknum = 0
		wait(0.5)
		debounce = false
		ticktime = tick()
		end
	end	
	--timed punch 3
	if attacknum == 2 then
		newtick = tick()
		if newtick - ticktime <= 1.5 then
		debounce = true
		require(Mscript).punch3()
		attacknum = 3
		wait(0.5)
		debounce = false
		ticktime = tick()
	end 
	--timeout punch 4
	elseif attacknum == 3 then
		newtick = tick()
		if newtick - ticktime > 1.5 then
		debounce = true
		require(Mscript).punch1()
		attacknum = 0
		wait(0.5)
		debounce = false
		ticktime = tick()
		end
	end
	--timed punch 4
	if attacknum == 3 then
		newtick = tick()
		if newtick - ticktime <= 1.5 then
		debounce = true
		require(Mscript).punch4()
		attacknum = 0
		wait(1)
		debounce = false
			
		end	
	end	
end)

please tell me what the error is and how i would reseolve it.

1 Like

I believe your elseifs aren’t being aligned properly. Please format your code clearly. There is no point in putting elseif after end.

You have an end after each if statement, which might be causing it.

What you have:

if condition then
end

elseif something then
end

Should be:

if condition then
    --stuff
elseif condition2 then
end

just like @iHasLag9 said.

i tried removing the "end"s after if statements, and the saem bug still happens.

local UIS = game:GetService("UserInputService")
local attacknum = 0
local Mscript = script.Parent.testimgm1m
local debounce = false
local newtick = nil
local ticktime = nil

UIS.InputBegan:Connect(function(input, istyping)
if input.UserInputType == Enum.UserInputType.MouseButton1 and not istyping and debounce == false then
	--first punch
	if attacknum == 0 then
		debounce = true
		require(Mscript).punch1()
		attacknum = 1
		wait(0.5)
		debounce = false
		ticktime = tick()
	
	--timeout punch 2
	elseif attacknum == 1 then
		newtick = tick()
		if newtick - ticktime > 1.5 then
		debounce = true
		require(Mscript).punch1()
		attacknum = 0
		wait(0.5)
		debounce = false
		ticktime = tick()
		end
	end
	--timed punch 2
	if attacknum == 1 then
		newtick = tick()
		if newtick - ticktime <= 1.5 then
		debounce = true
		require(Mscript).punch2()
		attacknum = 2
		wait(0.5)
		debounce = false
		ticktime = tick()
		
	--timeout punch 3
	elseif attacknum == 2 then
		newtick = tick()
		if newtick - ticktime > 1.5 then
		debounce = true
		require(Mscript).punch1()
		attacknum = 0
		wait(0.5)
		debounce = false
		ticktime = tick()
		end
	end	
	--timed punch 3
	if attacknum == 2 then
		newtick = tick()
		if newtick - ticktime <= 1.5 then
		debounce = true
		require(Mscript).punch3()
		attacknum = 3
		wait(0.5)
		debounce = false
		ticktime = tick()
	
	--timeout punch 4
	elseif attacknum == 3 then
		newtick = tick()
		if newtick - ticktime > 1.5 then
		debounce = true
		require(Mscript).punch1()
		attacknum = 0
		wait(0.5)
		debounce = false
		ticktime = tick()
		end
	end
	--timed punch 4
	if attacknum == 3 then
		newtick = tick()
		if newtick - ticktime <= 1.5 then
		debounce = true
		require(Mscript).punch4()
		attacknum = 0
		wait(1)
		debounce = false
			
			
					end
				end	
			end
		end	
	end	
end)

is this just formatted incorrectly, if so, how would i format it correctly here?

What exactly are you trying to achieve with this code? I see multiple of the same if statements.

Unnecessarily adding if after an exactly identical elseif can lead to problems.

if you dont punch again within 1.5 seconds, it will reset back to the first punch. thats why there are so many identical if statements.

1 Like

And this punch is initiated by calling it in its entirety each click.

You might consider something like this:

  1. Set an attribute Combo Initially
game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character:SetAttribute("Combo", 1)
		character:SetAttribute("GuardHealth", 4)
	end)
end)

2: Set a function to set the combo when called:

local function setCombo(char)
	local combo = char:GetAttribute("Combo")

	if combo < maxCombo then
		char:SetAttribute("Combo", combo+1)
	else
		char:SetAttribute("Combo", 1)
	end
end

3: Call SetCombo each click

	setCombo(char)

4: If your animation for punching stops, reset the combo with this function:
(inside of click detection)

	animation.Stopped:Connect(function() --Disables after a second of animation being stopped
		ComboReset(char, combo)
		
	end)

(outside of click detection)

local function ComboReset(character, oldcombo)
	task.delay(1, function()
		local currentCombo = character:GetAttribute("Combo")

		if oldcombo == 5 then return end

		if currentCombo - 1 == oldcombo and not character:GetAttribute("Attacking") then
			character:SetAttribute("Combo", 1)
		end

	end)
end

Try this and see if this setup helps. Incorporate your punching detection and logic alongside it.

Also consider having your click detection inside of a local script, place the code above this post inside of a server script and connect it with a remote event.

i dont usually like taking other people scripts, but im trying to understand yours so i can improve. “combo” attribute keeps showing up as nil. where did i mess up?

local remote = script.Parent["M1 event"]

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character:SetAttribute("Combo", 1)
		character:SetAttribute("GuardHealth", 4)
		print(character:GetAttribute("Combo"))
	end)
end)

local function setCombo(char)
	local combo = char:GetAttribute("Combo")

	if combo < 5 then
		char:SetAttribute("Combo", combo+1)
	else
		char:SetAttribute("Combo", 1)
	end
end

local function ComboReset(character, oldcombo)
	task.delay(1, function()
		local currentCombo = character:GetAttribute("Combo")

		if oldcombo == 5 then return end

		if currentCombo - 1 == oldcombo and not character:GetAttribute("Attacking") then
			character:SetAttribute("Combo", 1)
		end

	end)
end

remote.OnServerEvent:Connect(function(char)
	ComboReset(char)
	print(char:GetAttribute("Combo", 1))
		
	
end)

im sorry, im new.