If statement being fired even though its not true

Hello! Im making it so that a script carries a integer variable and the variable is added by 1 every time a punch happens. However, for one of my if statements, it says if currentPunch == 1 then. It fires, but then the currentpunch is 2 at the end of it. However, it still fires after when I try it again.

There are no errors in the output and I am really confused on why this is happening. Can you help? I would really appreciate it.

Thank you.

local currentPunch = 0

local debounce = false

local holdingkey = false

local startfightstuff = false

local function punch()
	local Blocking = character:FindFirstChild("Blocking").Value
	print(Blocking)

	if debounce then return end

	if Blocking then return end

	debounce = true

	if currentPunch == 0 then
		print('punch 0')
		hitboxevent:FireServer(Vector3.new(3, 3, 3), Vector3.new(0, 0, -3.5), math.random(3, 8), 0.3)
		rightPunch:Play()
		task.wait(0.5)
		debounce = false
		currentPunch += 1
		print(currentPunch)
	elseif currentPunch == 1 then
		print('punch 1')
		hitboxevent:FireServer(Vector3.new(3, 3, 3), Vector3.new(0, 0, -3.5), math.random(3, 8), 0.3)
		leftPunch:Play()
		task.wait(0.5)
		debounce = false
		currentPunch += 1
		print(currentPunch)
	elseif currentPunch == 2 then
		print('punch 2')
		hitboxevent:FireServer(Vector3.new(3, 3, 3), Vector3.new(0, 0, -3.5), math.random(3, 8), 0.3)
		rightPunch:Play()
		task.wait(0.5)
		debounce = false
		currentPunch += 1
		print(currentPunch)
1 Like

This code is really inefficient, just do

hitboxevent:FireServer(Vector3.new(3, 3, 3), Vector3.new(0, 0, -3.5), math.random(3, 8), 0.3)
if currentPunch == 0 then
rightPunch:Play()
elseif currentPunch == 2 then
rightPunch:Play()
task.wait(0.5)
debounce = false
currentPunch += 1
print(currentPunch)

etc etc

1 Like

Thank you but, how would this fix my problem? I don’t want code review I want to understand where it went wrong and how to fix it

What does the output look like with all those prints?

image

Ignore the falses, thats earlier in my code

Is there anywhere in your script where you may subtract from currentPunch?. And where is the script located, and if possible could we have the full script?

No, this is the only script that handles currentPunch

i think this should work

local PunchCounter = 1
local PunchDebounce = false

local PunchSounds = {
	rightPunch, -- 1
	leftPunch, -- 2
	rightPunch, --3
}

local function PlayPunchFromTable()
	local PunchSound = PunchSounds[PunchCounter]
	if PunchSound then
		PunchSound:Play()
		PunchCounter += 1
	else
		PunchCounter = 1
		PlayPunchFromTable()
	end
end

local function punch()
	
	if PunchDebounce then
		return
	else
		PunchDebounce = not PunchDebounce
	end

	local Blocking = character:FindFirstChild("Blocking").Value
	if Blocking then
		return
	end
	
	hitboxevent:FireServer(Vector3.new(3, 3, 3), Vector3.new(0, 0, -3.5), math.random(3, 8), 0.3)
	
	PlayPunchFromTable()
	
	task.wait(0.5)
	
	PunchDebounce = not PunchDebounce
end

All I would add as the debounce will break there.

that dont make sense, if someone were spam playing the function wouldnt it be bypassable because when blocking is off it sets it true? etc (i dont think it needs changing there

How would I add more to the table? Because in your way of doing it, your making rightpunch two times? Here is all my code:

local cas = game:GetService("ContextActionService")
local rs = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")

local events = rs:WaitForChild("Events")
local hitboxevent = events:WaitForChild("HitBox")

local plr = game.Players.LocalPlayer
local character = plr.Character or plr.CharacterAdded:Wait()
local hum = character:WaitForChild("Humanoid")
local animator = hum:WaitForChild("Animator")

local leftPunch = animator:LoadAnimation(script:WaitForChild("LeftPunch"))
local rightPunch = animator:LoadAnimation(script:WaitForChild("RightPunch"))
local middlePunch = animator:LoadAnimation(script:WaitForChild("MiddlePunch"))
local Block = animator:LoadAnimation(script:WaitForChild("Block"))
local Blockbreak = animator:LoadAnimation(script:WaitForChild("Blockbreak"))
local Blockhit = animator:LoadAnimation(script:WaitForChild("Blockhit"))
local Demproll = animator:LoadAnimation(script:WaitForChild("Demproll"))

local currentPunch = 0

local debounce = false

local holdingkey = false

local startfightstuff = false

local function punch()
	local Blocking = character:FindFirstChild("Blocking").Value
	print(Blocking)

	if debounce then return end

	if Blocking then return end

	debounce = true

	if currentPunch == 0 then
		print('punch 0')
		hitboxevent:FireServer(Vector3.new(3, 3, 3), Vector3.new(0, 0, -3.5), math.random(3, 8), 0.3)
		rightPunch:Play()
		task.wait(0.5)
		debounce = false
		currentPunch += 1
		print(currentPunch)
	elseif currentPunch == 1 then
		print('punch 1')
		hitboxevent:FireServer(Vector3.new(3, 3, 3), Vector3.new(0, 0, -3.5), math.random(3, 8), 0.3)
		leftPunch:Play()
		task.wait(0.5)
		debounce = false
		currentPunch += 1
		print(currentPunch)
	elseif currentPunch == 2 then
		print('punch 2')
		hitboxevent:FireServer(Vector3.new(3, 3, 3), Vector3.new(0, 0, -3.5), math.random(3, 8), 0.3)
		rightPunch:Play()
		task.wait(0.5)
		debounce = false
		currentPunch += 1
		print(currentPunch)
	elseif currentPunch == 3 then
		print('punch 3')
		hitboxevent:FireServer(Vector3.new(3, 3, 3), Vector3.new(0, 0, -3.5), math.random(3, 8), 0.3)
		middlePunch:Play()
		task.wait(0.5)
		debounce = false
		currentPunch += 1
	elseif character:FindFirstChild("HitCounter").Value == 10 then
		print('hello')
		hitboxevent:FireServer(Vector3.new(3, 3, 3), Vector3.new(0, 0, -3.5), 25, 0.3)
		print('done diddly doo')
		Demproll:Play()
		task.wait(1)
		debounce = false
	end

	if currentPunch == 3 then
		currentPunch = 0
	else
		currentPunch = 1
	end
end

local function functiontoblock(ready)
	if ready then
		game.ReplicatedStorage.BlockBool:FireServer(true)
		Block:Play()
		game.ReplicatedStorage.BlockSpeed:FireServer(true)
	elseif ready == false then
		game.ReplicatedStorage.BlockBool:FireServer(false)
		game.ReplicatedStorage.BlockSpeed:FireServer(false)
		Block:Stop()
		return
	end
end

UIS.InputBegan:Connect(function(input, gpe)
	if gpe then return end
	local Blocking = character:FindFirstChild("Blocking").Value
	if startfightstuff and input.UserInputType == Enum.UserInputType.MouseButton1 and not Blocking then
		punch()
	elseif input.KeyCode == Enum.KeyCode.Space and startfightstuff then
		holdingkey = true
		functiontoblock(true)
	end
end)

UIS.InputEnded:Connect(function(input, gpe)
	if gpe then return end
	if input.KeyCode == Enum.KeyCode.Space and startfightstuff then
		print('hi')
		holdingkey = false
		functiontoblock(false)
	end
end)

game.ReplicatedStorage.StartFightMoves.OnClientEvent:Connect(function(ready)
	if ready then
		startfightstuff = true
	elseif not ready then
		startfightstuff = false
	end
end)

game.ReplicatedStorage.IsBlocking.OnClientEvent:Connect(function(name, aretheyblocking)
	if name == plr.Name then
		if aretheyblocking then
			Blockhit:Play()
		end
	end
end)
local PunchSounds = {
	rightPunch, -- 1
	leftPunch, -- 2
	rightPunch, --3
	rightPunch,
	rightPunch,
	rightPunch,
	rightPunch,
	leftPunch,
	leftPunch,
	leftPunch,
	--infinity
}

? you can edit it

would rightPunch be an animation?

i thought it was a sound? but both use the :Play() feature so sure, itd work fine

1 Like

In the orginal script,

Debounce is changed after the fact. I’m not the OP, but your script will break entirely. Now you can do something like:

local Blocking = character:FindFirstChild("Blocking").Value

if PunchDebounce or Blocking then return end
	
PunchDebounce = not PunchDebounce --We change it AFTER our checks

But it’s isn’t the same as the topic because in your script:

if PunchDebounce then --We are assume it's the first time it is ran.
		return
	else
		PunchDebounce = not PunchDebounce --You change it to true
	end

	local Blocking = character:FindFirstChild("Blocking").Value
	if Blocking then --Were assuming its blocked in this case
		return --Debounce is never changed meaning its true. Meaning after it has been blocked, It will never run After "if PunchDebounce then" because PunchDebounce is stuck being true currently in your version
	end

way too extra


	if PunchDebounce then
		return
	end

	local Blocking = character:FindFirstChild("Blocking").Value
	if Blocking then
		return
	else
		PunchDebounce = not PunchDebounce
	end

or realistically just do

	if PunchDebounce then
		return
	end

	local Blocking = character:FindFirstChild("Blocking").Value
	if Blocking then
		return
	end
	
	PunchDebounce = not PunchDebounce

or I like

local Blocking = character:FindFirstChild("Blocking").Value

if PunchDebounce or Blocking then return end
	
PunchDebounce = not PunchDebounce

Whatever way is fine, but just understand that it is needed for your script to account the Debounce change, and you were changing it before it was blocked, meaning if it was blocked, it would forever be stuck at true. Above were just some examples

i recommend the way i said, bc les say that PunchDebounce is true, now we have no need to run character:FindFirstChild(“Blocking”).Value which would be inefficient to run without any reason

Actually no. Lua will stop a if statement if it is meet in anyway, Because of how Lua is ran from left to right, up to down

So if PunchDebounce was true, It will only run if PunchDebounce or, and since one requirement is meant, Luau moves on without checking Blocked.

This is a really small difference anyways. You can totally do both methods, but

if PunchDebounce then
		return
	end

	local Blocking = character:FindFirstChild("Blocking").Value
	if Blocking then
		return
	end

Is excatly the same as

local Blocking = character:FindFirstChild("Blocking").Value

if PunchDebounce or Blocking then return end --Order in this case matters and was intentional
	
PunchDebounce = not PunchDebounce


---Or if you really want and care you could do---



if PunchDebounce or character:FindFirstChild("Blocking").Value then return end --Order in this case matters and was intentional
local Blocking = character:FindFirstChild("Blocking").Value
	
PunchDebounce = not PunchDebounce

No difference in how it is ran, other then one has a little more lines of code.

But you can pick the one that suits you and understand better. Its extremely small differences and It really doesn’t matter!

1 Like