Damage script returning random values regardless if player is blocking?

I have a script that attacks the player and throws either left or right punches and does random damage. It’s suppose to do 0 damage when the player’s “IsBlocking” attribute is true, but for some reason the NPC will just ignore if the player IsBlocking or not and do 0 damage randomly.

Script:

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

local targetedPlayer = nil
local playerIsBlocking = nil

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)
		end
	end
end

local function GetDamageForPunchType(punchType)
	if targetedPlayer then
		playerIsBlocking = targetedPlayer:GetAttribute("IsBlocking")
		
		if playerIsBlocking == true then
			return 0
		elseif punchType == "LeftPunch" then
			return math.random(1, 2)
		elseif punchType == "RightPunch" then
			return math.random(1, 2)
		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

	targetedPlayer = nearestPlayer

	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:LoadAnimation(Animation):Play()
			local damage = GetDamageForPunchType(actionName)
			DealDamage(nearestPlayer, damage)
			Remote:InvokeClient(nearestPlayer)
		end
	end

	local function PunchLoop()
		while wait(1) do
			PlayPunch()
		end
	end

	task.spawn(PunchLoop)
end

local npc = script.Parent

while wait(math.random(1, 2)) 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

Maybe make it so it also checks if the player is not blocking

local function GetDamageForPunchType(punchType)
	if targetedPlayer then
		playerIsBlocking = targetedPlayer:GetAttribute("IsBlocking")
		
		if playerIsBlocking == true then
			return 0
		elseif punchType == "LeftPunch" and not playerIsBlocking then
			return math.random(1, 2)
		elseif punchType == "RightPunch" and not playerIsBlocking then
			return math.random(1, 2)
		end
	end
end

Same result., but the damage will 90% of the time return 0 along side left punch or right punch damage.

The attribute is set correctly, and I get the same result when I set it manually while in studio edit mode.

Yea I just said something incorrectly. I think it has to do with the PunchLoop function and PlayPunch since both kinda need the nearestPlayer

local function PlayPunch(nearestPlayer)
        local humanoid = npc:WaitForChild("Humanoid")
        humanoid:LoadAnimation(Animation):Play()

        local damage = GetDamageForPunchType(actionName)
        DealDamage(nearestPlayer, damage)
        Remote:InvokeClient(nearestPlayer)
    end

    local function PunchLoop()
        while wait(1) do
            local position = npc.PrimaryPart.Position
            local nearestPlayer = GetNearestPlayer(position, 5)

            if nearestPlayer then
                PlayPunch(nearestPlayer)
            end
        end
    end
1 Like

Is the script that sets the block value a local script? Local scripts don’t change how the server sees stuff. That might also explain how editing the value while playing the game didn’t work either. Anything you set while on the client is client sided.

Are you sure that the attribute is on the player and not character?
Try doing

playerIsBlocking = targetedPlayer.Character:GetAttribute("IsBlocking")
1 Like

Code looks working, your problem will be in the attribute self. The code for it is missing, so provide us how you set the IsBlocking attribute.

Attributes this way (clicking on it) are mostly not replicated, so your attribute isn’t on the server.

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