Why is it dealing massive damage to the block?

so i am making a game where players build a base and a chosen player gets to place down minions to raid the base

but when i first tested the minion, it dealt massive damage (more than allowed), and then never damaged the blocks again…
why?

script for npc:

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

local humanoid = script.Parent
local root = humanoid.Parent.PrimaryPart

local targetDistance = script:GetAttribute("TargetDistance")
local stopDistance = script:GetAttribute("StopDistance")
local damage = script:GetAttribute("Damage")
local attackDistance = script:GetAttribute("AttackDistance")
local attackWait = script:GetAttribute("AttackWait")
local lastAttack = tick()

local HasDamaged = false

function findNearestPlayer()
	local playerList = Players:GetPlayers()
	
	local nearestPlayer = nil
	local distance = nil
	local direction = nil
	
	for i, player in pairs(playerList) do
		local character = player.Character
		if character then
			local distanceVector = (player.Character.HumanoidRootPart.Position - root.Position)
			if not nearestPlayer then
				nearestPlayer = player
				distance = distanceVector.Magnitude
				direction = distanceVector.Unit
			elseif distanceVector.Magnitude < distance then
				nearestPlayer = player
				distance = distanceVector.Magnitude
				direction = distanceVector.Unit
			end	
		end
	end
	
	return nearestPlayer, distance, direction
end

RunService.Heartbeat:Connect(function()
	local nearestPlayer, distance, direction = findNearestPlayer()
	if nearestPlayer then
		if distance <= targetDistance and distance >= stopDistance then
			humanoid:Move(direction)
		else
			humanoid:Move(Vector3.new())
		end
		
		if distance <= attackDistance and tick() - lastAttack >= attackWait then
			lastAttack = tick()
			nearestPlayer.Character.Humanoid.Health -= damage
		end
	end
end)

for i,v in pairs(humanoid.Parent:GetChildren()) do
	if v:IsA("Part") or v:IsA("MeshPart") then 
		v.Touched:Connect(function(hit)
			if HasDamaged then return end
			if not hit.Name.match(hit.Name,"Block",0) then return end
			local Dmg = damage - hit:GetAttribute("Defense")
			if Dmg <= 0 then return end
			hit:SetAttribute("Health",hit:GetAttribute("Health") - damage)
			HasDamaged = true
			task.wait(5)
			HasDamaged = false
		end)
	end
end

script for the block:

local Part = script.Parent
local MaxHealth = Part:GetAttribute("MaxHealth")
local Defense = Part:GetAttribute("Defense")

Part.AttributeChanged:Connect(function(attr)
	if attr ~= "Health" then return end
	Part.Transparency = Part:GetAttribute(attr) / MaxHealth
	print(Part:GetAttribute("Health"))
	if Part:GetAttribute(attr) <= 0 then
		Part:Destroy()
	end
end)

damage for the minion is 3 btw

Thanks in advance!

It should be like this or damage should be hanged to Dmg?

1 Like

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