Still processed for some reason

So I have an if statement that should return as end, but for some reason it doesn’t return as end and always has the same result for some reason.
Line:
if hit.Parent:FindFirstChild("Humanoid") == nil then return end
Even if I do:
if hit.Parent:FindFirstChild("Humanoid") == nil then return end
It still has the same result. For some reason.
Full Script:

local function RightTouched()
	Player.Character.RightHand.Touched:Connect(function(hit)
		if DamageStopper == false then
			if hit.Parent:FindFirstChild("Humanoid") == nil then return end
				local TargetHumanoid = hit.Parent:FindFirstChild("Humanoid")
				DamageStopper = true
				TargetHumanoid:TakeDamage(Damage)
		end	
	end)
end

Thanks for reading.

Firstly, why are you using an event within a function? It would be better if you did something like this:

local function RightTouched(hit)
   if DamageStopper == false then
			if hit.Parent:FindFirstChild("Humanoid") == nil then return end
				local TargetHumanoid = hit.Parent:FindFirstChild("Humanoid")
				DamageStopper = true
				TargetHumanoid:TakeDamage(Damage)
		end	
end

Player.Character.RightHand.Touched:Connect(RightTouched)

Secondly, we can’t see your full script so it’s hard to say why it’s returning as it is. This could be for several reasons:

  • DamageStopper may be true
  • Humanoid may be present
  • The function never fired because of my first reason
1 Like

I use an event inside a function because I need to do the same thing multiple times.

Here’s the whole script:

local Event = script.Parent:WaitForChild("RemoteEvent")

local CombatCount = 1
local CanAttack = true
local Cooldown = 0.25

local Damage = 10
local DamageStopper = false
local DamageCooldwon = 0.4

Event.OnServerEvent:Connect(function(Player)
	DamageStopper = false
	
	local Animations = {
		RightPunch = script.Parent.Animations:WaitForChild("RightPunch"),
		LeftPunch = script.Parent.Animations:WaitForChild("LeftPunch"),
	}

	
	local function RightTouched()
		Player.Character.RightHand.Touched:Connect(function(hit)
			if DamageStopper == false then
				if hit.Parent:FindFirstChild("Humanoid") == nil then return end
					local TargetHumanoid = hit.Parent:FindFirstChild("Humanoid")
					DamageStopper = true
					TargetHumanoid:TakeDamage(Damage)
			end	
		end)
	end
	
	
	local function LeftTouched()
		Player.Character.LeftHand.Touched:Connect(function(hit)
			if DamageStopper == false then
				if hit.Parent:FindFirstChild("Humanoid") == nil then return end
					local TargetHumanoid = hit.Parent:FindFirstChild("Humanoid")
					DamageStopper = true
					TargetHumanoid:TakeDamage(Damage)
			end	
		end)
	end


	if CombatCount == 1 and CanAttack == true then
			RightTouched()
			local RightPunch = Player.Character.Humanoid:LoadAnimation(Animations.RightPunch)
			RightPunch:Play()
			CombatCount = 2
			CanAttack = false
			wait(Cooldown)
			CanAttack = true
	elseif CombatCount == 2 and CanAttack == true then
			LeftTouched()
			local RightPunch = Player.Character.Humanoid:LoadAnimation(Animations.LeftPunch)
			RightPunch:Play()
			CombatCount = 1
			CanAttack = false
			wait(Cooldown)
			CanAttack = true
	end
end)