Deal 0 damage if plasyer has RightBlocking or LeftBlocking BoolValue

So i’ve tried to make a script that deals 0 damage when the player is blocking left and the npc throws a right punch and vice versa (player will have a BoolValue in them called RightBlocking or LeftBlocking), but i’ve tried like 10 different methods for detecting this and they all just don’t work or completely break the script. I’d assume the code would be quite simple but I can’t really figure it out.

Full Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local function DealDamage(player, damage)
	local character = player.Character
	if character then
		local humanoid = character:FindFirstChild("Humanoid")
		if humanoid then
			humanoid.Health = humanoid.Health - damage
			print("Damage applied:", damage)
		end
	end
end

local function GetDamageForPunchType(punchType)
	if punchType == "LeftPunch" then
		return math.random(3, 8)
	elseif punchType == "RightPunch" then
		return math.random(10, 25)
	end
end

local function GetNearestPlayer(position, maxDistance)
	local players = Players:GetPlayers()
	local nearestPlayer
	local shortestDistance = maxDistance

	for _, player in pairs(players) do
		local character = player.Character
		if character and character:FindFirstChild("Humanoid") then
			local distance = (position - character:WaitForChild("Humanoid").Parent.PrimaryPart.Position).Magnitude

			if distance < shortestDistance then
				nearestPlayer = player
				shortestDistance = distance
			end
		end
	end

	return nearestPlayer
end

local function ThrowPunch(npc, actionName, animationId)
	local Remote = ReplicatedStorage:WaitForChild(actionName .. "Remote")
	local Animation = Instance.new("Animation")
	Animation.AnimationId = animationId

	local damage = GetDamageForPunchType(actionName)  -- Get the damage for the specific punch type

	local function PlayPunch()
		local humanoid = npc:WaitForChild("Humanoid")
		local position = humanoid.Parent.PrimaryPart.Position
		local nearestPlayer = GetNearestPlayer(position, 5)

		if nearestPlayer then
			humanoid:LoadAnimation(Animation):Play()
			DealDamage(nearestPlayer, damage)
			Remote:InvokeClient(nearestPlayer)
		end
	end

	local function PunchLoop()
		while true do
			PlayPunch()
			wait(math.random(2, 6))  -- Wait for a break before the next punch
		end
	end

	-- Start the coroutine
	task.spawn(PunchLoop)
end

local npc = script.Parent

while wait(1) do
	local punchTypes = {
		{ type = "LeftPunch", animationId = "rbxassetid://15479380094" },
		{ type = "RightPunch", animationId = "rbxassetid://15476721938" }
	}

	local punchInfo = punchTypes[math.random(1, #punchTypes)]
	ThrowPunch(npc, punchInfo.type, punchInfo.animationId)
end

Damage Part of Script:

local function GetDamageForPunchType(punchType)
	if punchType == "LeftPunch" then
		return math.random(3, 8)
	elseif punchType == "RightPunch" then
		return math.random(10, 25)
	end
end

You can check if their RightBlocking/LeftBlocking value is true. There are a few other things too. You should be using task.wait instead of wait, because it’s more accurate and efficient. You can also do value -= 2 instead of value = value - 2. You should also be using Animator to load animations instead of the humanoid.

Code:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local function DealDamage(player, damage)
	local character = player.Character
	if character then
		local humanoid = character:FindFirstChild("Humanoid")
		if humanoid then
			humanoid.Health -= damage
			print("Damage applied:", damage)
		end
	end
end

local function GetDamageForPunchType(player, punchType)
	if punchType == "LeftPunch" then
		if player.LeftBlocking.Value then
			return 0
		else
			return math.random(3, 8)
		end
	elseif punchType == "RightPunch" then
		if player.RightBlocking.Value then
			return 0
		else
			return math.random(10, 25)		
		end
	end
end

local function GetNearestPlayer(position, maxDistance)
	local players = Players:GetPlayers()
	local nearestPlayer
	local shortestDistance = maxDistance

	for _, player in pairs(players) do
		local character = player.Character
		if character and character:FindFirstChild("Humanoid") then
			local distance = (position - character:WaitForChild("Humanoid").Parent.PrimaryPart.Position).Magnitude

			if distance < shortestDistance then
				nearestPlayer = player
				shortestDistance = distance
			end
		end
	end

	return nearestPlayer
end

local function ThrowPunch(npc, actionName, animationId)
	local Remote = ReplicatedStorage:WaitForChild(actionName .. "Remote")
	local Animation = Instance.new("Animation")
	Animation.AnimationId = animationId

	local function PlayPunch()
		local humanoid = npc:WaitForChild("Humanoid")
		local position = humanoid.Parent.PrimaryPart.Position
		local nearestPlayer = GetNearestPlayer(position, 5)

		if nearestPlayer then
			humanoid.Animator:LoadAnimation(Animation):Play()
			
			local damage = GetDamageForPunchType(nearestPlayer, actionName)  -- Get the damage for the specific punch type
			DealDamage(nearestPlayer, damage)
			Remote:InvokeClient(nearestPlayer)
		end
	end

	local function PunchLoop()
		while true do
			PlayPunch()
			task.wait(math.random(2, 6))  -- Wait for a break before the next punch
		end
	end

	-- Start the coroutine
	task.spawn(PunchLoop)
end

local npc = script.Parent

while task.wait(1) do
	local punchTypes = {
		{ type = "LeftPunch", animationId = "rbxassetid://15479380094" },
		{ type = "RightPunch", animationId = "rbxassetid://15476721938" }
	}

	local punchInfo = punchTypes[math.random(1, #punchTypes)]
	ThrowPunch(npc, punchInfo.type, punchInfo.animationId)
end

honestly for that while loop you can just do

while task.wait(math.random(2,6)) do
    PlayPunch()
end
1 Like

That would wait before punching for the first iteration. It wouldn’t be noticeable but it also wouldn’t do the same thing as the OP’s code.

1 Like