Sword does less damage to blocking players or npcs

So I’m making a rpg game
What I’m trying to do is when a player or a npc is blocking the sword does less damage
But the sword still does the same damage
It does not give me any errors
The code:

-- Variables


local db = false

local damage = script.Parent.Damage.Value
	
local canHit = false

local randomSlash = 1

local idle
local slash
local running

-- Configure
local con = {
	trailEnabled = true;
	bloodEffects = true
}

-- Player Equip
script.Parent.Equipped:Connect(function()
	script.Parent.Hitbox.Equip:Play()
	local humanoid = script.Parent.Parent:FindFirstChild("Humanoid")
	
	if not idle then idle = humanoid:LoadAnimation(script.Equip) end

	idle:Play()
end)

-- The sword slashes
script.Parent.Activated:Connect(function()
	if not db then
		db = true
		if con.trailEnabled == true then
			script.Parent.Hitbox.Trail.Enabled = true
		end
		local humanoid = script.Parent.Parent:FindFirstChild("Humanoid")
		if not slash then
			if randomSlash == 1 then
				
				slash = humanoid:LoadAnimation(script.Slash1)
				randomSlash = randomSlash + 1
				wait(0.2)
			elseif randomSlash == 2 then
				slash = humanoid:LoadAnimation(script.Slash2)
				randomSlash = randomSlash + 1
				wait(0.2)
			elseif randomSlash == 3 then
			
				slash = humanoid:LoadAnimation(script.Slash3)
				randomSlash = randomSlash - 2
			end
		end
		slash:Play()
		wait(.2)
		script.Parent.Hitbox.Slash:Play()
		wait(.8)
		if slash then
			slash:Stop()
			slash = nil
		end
		script.Parent.Hitbox.Trail.Enabled = false
		db = false
	end
end)

-- If the sword slash, the damage meets
script.Parent.Hitbox.Touched:Connect(function(hit)
	if db then
		if not canHit then
			canHit = true
			local effect = script.Parent.Hitbox.Effect:Clone()
			if hit.Parent:FindFirstChild("Humanoid") then
				local hitSFX = Instance.new("Sound",hit.Parent.Head); hitSFX.SoundId = "rbxassetid://566593606"; hitSFX:Play()
				local block = hit.Parent:FindFirstChild("block")
				if not block then
					damage = 10
					hit.Parent.Humanoid:TakeDamage(damage)
					if con.bloodEffects == true then
						effect.Parent = hit.Parent.HumanoidRootPart
						effect.Enabled = true
					end
					
					elseif block then
						if block == true then
						damage = 5
						wait(0.2)
						hit.Parent.Humanoid:TakeDamage(damage)
						if con.bloodEffects == true then
							effect.Parent = hit.Parent.HumanoidRootPart
								effect.Enabled = true
						elseif block == false then
							
									damage = 10
									hit.Parent.Humanoid:TakeDamage(damage)
									if con.bloodEffects == true then
										effect.Parent = hit.Parent.HumanoidRootPart
								effect.Enabled = true
								end
							
					end
					end
						end
					end
				
			
				
			wait(1)
			effect:Destroy()
			canHit = false
		end
	end


	-- Stops Everything when we unequip

			
script.Parent.Unequipped:Connect(function()
			
	if idle then idle:Stop() end
			if slash then slash:Stop() end
			end)
			end)
			

Can someone tell me what I’m doing wrong?

1 Like

is block a bool value?

If it is u should change

to

if block.Value == false

Yes it is a bool value
I changed it a bit tho it still isn’t working

If not block then

Its looking for “block”
So if it didn’t find it it will run some code

-- Variables


local db = false

local damage = script.Parent.Damage.Value
	
local canHit = false

local randomSlash = 1

local idle
local slash
local running

-- Configure
local con = {
	trailEnabled = true;
	bloodEffects = true
}

-- Player Equip
script.Parent.Equipped:Connect(function()
	script.Parent.Hitbox.Equip:Play()
	local humanoid = script.Parent.Parent:FindFirstChild("Humanoid")
	
	if not idle then idle = humanoid:LoadAnimation(script.Equip) end

	idle:Play()
end)

-- The sword slashes
script.Parent.Activated:Connect(function()
	if not db then
		db = true
		if con.trailEnabled == true then
			script.Parent.Hitbox.Trail.Enabled = true
		end
		local humanoid = script.Parent.Parent:FindFirstChild("Humanoid")
		if not slash then
			if randomSlash == 1 then
				
				slash = humanoid:LoadAnimation(script.Slash1)
				randomSlash = randomSlash + 1
				wait(0.2)
			elseif randomSlash == 2 then
				slash = humanoid:LoadAnimation(script.Slash2)
				randomSlash = randomSlash + 1
				wait(0.2)
			elseif randomSlash == 3 then
			
				slash = humanoid:LoadAnimation(script.Slash3)
				randomSlash = randomSlash - 2
			end
		end
		slash:Play()
		wait(.2)
		script.Parent.Hitbox.Slash:Play()
		wait(.8)
		if slash then
			slash:Stop()
			slash = nil
		end
		script.Parent.Hitbox.Trail.Enabled = false
		db = false
	end
end)

-- If the sword slash, the damage meets
script.Parent.Hitbox.Touched:Connect(function(hit)
	if db then
		if not canHit then
			canHit = true
			local effect = script.Parent.Hitbox.Effect:Clone()
			if hit.Parent:FindFirstChild("Humanoid") then
				local hitSFX = Instance.new("Sound",hit.Parent.Head); hitSFX.SoundId = "rbxassetid://566593606"; hitSFX:Play()
				local block = hit.Parent:FindFirstChild("block")
				if block.Value == false then
					damage = 10
					hit.Parent.Humanoid:TakeDamage(damage)
					if con.bloodEffects == true then
						effect.Parent = hit.Parent.HumanoidRootPart
						effect.Enabled = true
					end
					
					elseif block then
						if block.Value == true then
						damage = 5
						wait(0.2)
						hit.Parent.Humanoid:TakeDamage(damage)
						if con.bloodEffects == true then
							effect.Parent = hit.Parent.HumanoidRootPart
								effect.Enabled = true
						elseif block == false then
							
									damage = 10
									hit.Parent.Humanoid:TakeDamage(damage)
									if con.bloodEffects == true then
										effect.Parent = hit.Parent.HumanoidRootPart
								effect.Enabled = true
								end
							
					end
					end
						end
					end
				
			
				
			wait(1)
			effect:Destroy()
			canHit = false
		end
	end


	-- Stops Everything when we unequip

			
script.Parent.Unequipped:Connect(function()
			
	if idle then idle:Stop() end
			if slash then slash:Stop() end
			end)
			end)

maybe

-- Variables


local db = false

local damage = script.Parent.Damage.Value
	
local canHit = false

local randomSlash = 1

local idle
local slash
local running

-- Configure
local con = {
	trailEnabled = true;
	bloodEffects = true
}

-- Player Equip
script.Parent.Equipped:Connect(function()
	script.Parent.Hitbox.Equip:Play()
	local humanoid = script.Parent.Parent:FindFirstChild("Humanoid")
	
	if not idle then idle = humanoid:LoadAnimation(script.Equip) end

	idle:Play()
end)

-- The sword slashes
script.Parent.Activated:Connect(function()
	if not db then
		db = true
		if con.trailEnabled == true then
			script.Parent.Hitbox.Trail.Enabled = true
		end
		local humanoid = script.Parent.Parent:FindFirstChild("Humanoid")
		if not slash then
			if randomSlash == 1 then
				
				slash = humanoid:LoadAnimation(script.Slash1)
				randomSlash = randomSlash + 1
				wait(0.2)
			elseif randomSlash == 2 then
				slash = humanoid:LoadAnimation(script.Slash2)
				randomSlash = randomSlash + 1
				wait(0.2)
			elseif randomSlash == 3 then
			
				slash = humanoid:LoadAnimation(script.Slash3)
				randomSlash = randomSlash - 2
			end
		end
		slash:Play()
		wait(.2)
		script.Parent.Hitbox.Slash:Play()
		wait(.8)
		if slash then
			slash:Stop()
			slash = nil
		end
		script.Parent.Hitbox.Trail.Enabled = false
		db = false
	end
end)

-- If the sword slash, the damage meets
script.Parent.Hitbox.Touched:Connect(function(hit)
	if db then
		if not canHit then
			canHit = true
			local effect = script.Parent.Hitbox.Effect:Clone()
			if hit.Parent:FindFirstChild("Humanoid") then
				local hitSFX = Instance.new("Sound",hit.Parent.Head); hitSFX.SoundId = "rbxassetid://566593606"; hitSFX:Play()
				local block = hit.Parent:FindFirstChild("block")
				if not block then
					damage = 10
					hit.Parent.Humanoid:TakeDamage(damage)
					if con.bloodEffects == true then
						effect.Parent = hit.Parent.HumanoidRootPart
						effect.Enabled = true
					end
					
					elseif block then
						if block.Value == true then
						damage = 5
						wait(0.2)
						hit.Parent.Humanoid:TakeDamage(damage)
						if con.bloodEffects == true then
							effect.Parent = hit.Parent.HumanoidRootPart
								effect.Enabled = true
						elseif block == false then
							
									damage = 10
									hit.Parent.Humanoid:TakeDamage(damage)
									if con.bloodEffects == true then
										effect.Parent = hit.Parent.HumanoidRootPart
								effect.Enabled = true
								end
							
					end
					end
						end
					end
				
			
				
			wait(1)
			effect:Destroy()
			canHit = false
		end
	end


	-- Stops Everything when we unequip

			
script.Parent.Unequipped:Connect(function()
			
	if idle then idle:Stop() end
			if slash then slash:Stop() end
			end)
			end)

I changed the code a bit now it does damage but the is a problem
When I hit a person blocking the damage value goes to 5 but if I hit a person again without blocking the value does not go back to 10
code:

-- Variables


local db = false
local UIS = game:GetService("UserInputService")

local damage = script.Parent.Damage.Value
	
local canHit = false

local randomSlash = 1

local idle
local slash
local running

-- Configure
local con = {
	trailEnabled = true;
	bloodEffects = true
}

-- Player Equip
script.Parent.Equipped:Connect(function()
	script.Parent.Hitbox.Equip:Play()
	local humanoid = script.Parent.Parent:FindFirstChild("Humanoid")
	
	if not idle then idle = humanoid:LoadAnimation(script.Equip) end

	idle:Play()
end)

-- The sword slashes
script.Parent.Activated:Connect(function()
	if not db then
		db = true
		if con.trailEnabled == true then
			script.Parent.Hitbox.Trail.Enabled = true
		end
		local humanoid = script.Parent.Parent:FindFirstChild("Humanoid")
		if not slash then
			if randomSlash == 1 then
				
				slash = humanoid:LoadAnimation(script.Slash1)
				randomSlash = randomSlash + 1
				wait(0.2)
			elseif randomSlash == 2 then
				slash = humanoid:LoadAnimation(script.Slash2)
				randomSlash = randomSlash + 1
				wait(0.2)
			elseif randomSlash == 3 then
			
				slash = humanoid:LoadAnimation(script.Slash3)
				randomSlash = randomSlash - 2
			end
		end
		slash:Play()
		wait(.2)
		script.Parent.Hitbox.Slash:Play()
		wait(.8)
		if slash then
			slash:Stop()
			slash = nil
		end
		script.Parent.Hitbox.Trail.Enabled = false
		db = false
	end
end)

-- If the sword slash, the damage meets
script.Parent.Hitbox.Touched:Connect(function(hit)
	if db then
		if not canHit then
			canHit = true
			local effect = script.Parent.Hitbox.Effect:Clone()
			if hit.Parent:FindFirstChild("Humanoid") then
				local hitSFX = Instance.new("Sound",hit.Parent.Head); hitSFX.SoundId = "rbxassetid://566593606"; hitSFX:Play()
				local block = hit.Parent.Humanoid:FindFirstChild("block")
				if not block.Value then
					hit.Parent.Humanoid:TakeDamage(damage)
					if con.bloodEffects == true then
						effect.Parent = hit.Parent.HumanoidRootPart
						effect.Enabled = true
					end
					
				elseif block.Value then
						if block.Value == true then
						damage = 5
						wait(0.2)
						hit.Parent.Humanoid:TakeDamage(damage)
						if con.bloodEffects == true then
							effect.Parent = hit.Parent.HumanoidRootPart
								effect.Enabled = true
						elseif block.Value == false then
									damage = 10
									hit.Parent.Humanoid:TakeDamage(damage)
									if con.bloodEffects == true then
										effect.Parent = hit.Parent.HumanoidRootPart
								effect.Enabled = true
								end
							
					end
					end
						end
					end
				
			
				
			wait(1)
			effect:Destroy()
			canHit = false
		end
	end


	-- Stops Everything when we unequip

			
script.Parent.Unequipped:Connect(function()
			
	if idle then idle:Stop() end
			if slash then slash:Stop() end
			end)
			end)
			
		
	




This code makes it still do 10 damage to both of them, blocking and unblocking players/npcs

Heres an example of the issue that i’m having(sorry for the bad quality)
The damage value stays at 5 damage
robloxapp-20211229-1938449.wmv (3.5 MB)

Code fixed

-- Variables


local db = false
local UIS = game:GetService("UserInputService")

local damage = script.Parent.Damage.Value
	
local canHit = false

local randomSlash = 1

local idle
local slash
local running

-- Configure
local con = {
	trailEnabled = true;
	bloodEffects = true
}

-- Player Equip
script.Parent.Equipped:Connect(function()
	script.Parent.Hitbox.Equip:Play()
	local humanoid = script.Parent.Parent:FindFirstChild("Humanoid")
	
	if not idle then idle = humanoid:LoadAnimation(script.Equip) end

	idle:Play()
end)

-- The sword slashes
script.Parent.Click.OnServerEvent:Connect(function()
	if not db then
		db = true
		if con.trailEnabled == true then
			script.Parent.Hitbox.Trail.Enabled = true
		end
		local humanoid = script.Parent.Parent:FindFirstChild("Humanoid")
		if not slash then
			if randomSlash == 1 then
				
				slash = humanoid:LoadAnimation(script.Slash1)
				randomSlash = randomSlash + 1
				wait(0.2)
			elseif randomSlash == 2 then
				slash = humanoid:LoadAnimation(script.Slash2)
				randomSlash = randomSlash + 1
				wait(0.2)
			elseif randomSlash == 3 then
			
				slash = humanoid:LoadAnimation(script.Slash3)
				randomSlash = randomSlash - 2
			end
		end
		slash:Play()
		wait(.2)
		script.Parent.Hitbox.Slash:Play()
		wait(.8)
		if slash then
			slash:Stop()
			slash = nil
		end
		script.Parent.Hitbox.Trail.Enabled = false
		db = false
		
	end
end)

-- If the sword slash, the damage meets
script.Parent.Hitbox.Touched:Connect(function(hit)
	if db then
		if not canHit then
			canHit = true
			local effect = script.Parent.Hitbox.Effect:Clone()
			if hit.Parent:FindFirstChild("Humanoid") then
				local hitSFX = Instance.new("Sound",hit.Parent.Head); hitSFX.SoundId = "rbxassetid://566593606"; hitSFX:Play()
				local block = hit.Parent:FindFirstChild("block")
				if not block.Value then
					damage = 10
					hit.Parent.Humanoid:TakeDamage(damage)
					if con.bloodEffects == true then
						effect.Parent = hit.Parent.HumanoidRootPart
						effect.Enabled = true
					end
					
				elseif block.Value then
						if block.Value == true then
						damage = 5
						wait(0.2)
						hit.Parent.Humanoid:TakeDamage(damage)
						if con.bloodEffects == true then
							effect.Parent = hit.Parent.HumanoidRootPart
								effect.Enabled = true
						elseif block.Value == false then
									damage = 10
									hit.Parent.Humanoid:TakeDamage(damage)
									if con.bloodEffects == true then
										effect.Parent = hit.Parent.HumanoidRootPart
								effect.Enabled = true
								end
							
					end
					end
						end
					end
				
			
				
			wait(1)
			effect:Destroy()
			canHit = false
		end
	end


	-- Stops Everything when we unequip

			
script.Parent.Unequipped:Connect(function()
			
	if idle then idle:Stop() end
			if slash then slash:Stop() end
			end)
			end)