While loop doesn't break when bool is false

this is a similar problem to my last post (no one knows about it but that’s okay)

basically, i have a while loop, and that while loop is meant to break if attacking is set to false

attacking is set to false when LMB is released, or when the tool is unequipped

however, the while loop continues, even when LMB gets released (doesn’t when the tool gets unequipped because tool isn’t a child of character anymore)

i’ll comment out the unnecessary stuff to save your sanity


client sided code that fires both events:

--local plr = game:GetService("Players").LocalPlayer
--local char = plr.Character or plr.CharacterAdded:Wait()
--local humanoid = char:WaitForChild("Humanoid")
--local katana = script.Parent

-- // MOUSE STUFF
--local mouse = plr:GetMouse()

-- // ANIMATIONS AND ANIMATOR
--local Combo = katana:WaitForChild("ComboAnim")
--local animator = humanoid:FindFirstChildOfClass("Animator")


-- // EVENTS AND COOLDOWNS
--local RS = game:GetService("ReplicatedStorage")
local ComboEvent = RS:WaitForChild("MeleeEvents").ComboEvent
local DisableAttackingEvent = RS:WaitForChild("MeleeEvents").DisableAttackingEvent

local Attacking = katana:WaitForChild("Attacking").Value
--local Stamina = plr:WaitForChild("Stamina").Value

-- // MISC
local connection = nil


-- // FUNCTION

--katana.Equipped:Connect(function()
	--connection = mouse.Button1Down:Connect(function()
		--if Attacking then return end
		--if Stamina < 9 then return end
		
		--local KatanaCombo = animator:LoadAnimation(Combo)

		--KatanaCombo:Play()		
		ComboEvent:FireServer() -- activates the while loop

		katana.Unequipped:Connect(function()
			KatanaCombo:Stop()
			connection:Disconnect()
		end)
		
		mouse.Button1Up:Connect(function()
			KatanaCombo:Stop()
			DisableAttackingEvent:FireServer() -- fires the disabling event
			connection:Disconnect()
		end)
	--end)
--end)



server sided code that handles the entire combo (and contains the while loop):

local ComboEvent = game:GetService("ReplicatedStorage"):WaitForChild("MeleeEvents").ComboEvent
local meleeInfo = require(game:GetService("ServerScriptService"):WaitForChild("Weapons"):WaitForChild("Melee").MeleeStats)
local DamageIndicatorEvent = game:GetService("ReplicatedStorage"):WaitForChild("PlayerEvents").DamageGUI
local Players = game:GetService("Players")

ComboEvent.OnServerEvent:Connect(function(player)
	local tool = player.Character:FindFirstChildOfClass("Tool")
	local attacking = tool.Attacking.Value
	local stamina = player.Stamina.Value
	
	--local COMBO_STAMINA_CONSUMPTION = meleeInfo[tool.Name]["COMBO_STAMINA_CONSUMPTION"]
	--local SwingSound = tool.Blade.SwingSound
	
	--if stamina < COMBO_STAMINA_CONSUMPTION then player:Kick("It did not need to end this way.") return end
	--if attacking then player:Kick("Do you really think I am that gullible?") return end
	
	
	attacking = true
	
	--local COMBO_DAMAGE = meleeInfo[tool.Name]["COMBO_DAMAGE"]
	--local COMBO_FIRST_DAMAGE_TIME = meleeInfo[tool.Name]["COMBO_FIRST_DAMAGE_TIME"]
	--local COMBO_CHECKPOINT_ONE = meleeInfo[tool.Name]["COMBO_CHECKPOINT_ONE"]
	--local COMBO_SECOND_DAMAGE_TIME = meleeInfo[tool.Name]["COMBO_SECOND_DAMAGE_TIME"]
	--local COMBO_CHECKPOINT_TWO = meleeInfo[tool.Name]["COMBO_CHECKPOINT_TWO"]
	--local COMBO_THIRD_DAMAGE_TIME = meleeInfo[tool.Name]["COMBO_THIRD_DAMAGE_TIME"]
	--local COMBO_FINISH = meleeInfo[tool.Name]["COMBO_FINISH"]
	
	local hitEnemies = {}
	
	local function DealDamage()
		local BOX_SIZE = meleeInfo[tool.Name]["BOX_SIZE"]
		local OFFSET = player.Character:FindFirstChild("HumanoidRootPart").CFrame * CFrame.new(0,0,-4)
		
		local BOX = workspace:GetPartBoundsInBox(OFFSET, BOX_SIZE)
		SwingSound:Play()
		
		for _,v in BOX do
			local otherPlayer = Players:GetPlayerFromCharacter(v.Parent)
			
			if v.Parent:FindFirstChild("Humanoid") and otherPlayer == nil and not table.find(hitEnemies, v.Parent) then
				table.insert(hitEnemies, v.Parent)
				
				local enemy = v.Parent:FindFirstChild("Humanoid")
				enemy:TakeDamage(COMBO_DAMAGE)
				DamageIndicatorEvent:FireClient(player, COMBO_DAMAGE, enemy)
			end
		end
	end
	
	
	tool.Unequipped:Connect(function()
		attacking = false
	end)
	
	
	while attacking do
		if not attacking then break end
		task.wait(COMBO_FIRST_DAMAGE_TIME)
		
		if not attacking then break end
		DealDamage()
		print("First damage")
		
		task.wait(COMBO_CHECKPOINT_ONE)
		if not attacking then break end
		print("First checkpoint")
		
		task.wait(COMBO_SECOND_DAMAGE_TIME)
		if not attacking then break end
		DealDamage()
		print("Second damage")
		
		task.wait(COMBO_CHECKPOINT_TWO)
		if not attacking then break end
		print("Second checkpoint")
		
		task.wait(COMBO_THIRD_DAMAGE_TIME)
		DealDamage() 
		print("Third damage")
		
		task.wait(COMBO_FINISH)
		attacking = false
	end
end)

server sided code that disables attacking:

local DisableAttackingEvent = game:GetService("ReplicatedStorage"):WaitForChild("MeleeEvents").DisableAttackingEvent

DisableAttackingEvent.OnServerEvent:Connect(function(player)
	local tool = player.Character:FindFirstChildOfClass("Tool")
	local attacking = tool.Attacking.Value
	
	attacking = false
end)

the results of these beautiful lines of code are rather underwhelming:

bumping as i still havent found a fix

Hello, it appears that you’re trying to set the boolean to false. However, you’re only setting the value of the variable “attacking” to false, which is not the same thing.

This means the BoolValue.Value will still return the default value it has, which is true in this case because of what I mentioned above.

To make things clearer, here’s an example: I wrote this code to test in SSS, and it gives the default value:

Code

Results

To fix this issue, you’ll need to set the BoolValue to false by using Attacking.Value = false instead of setting the variable to false. Hope this helps!

1 Like

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