I need help with a script

Ok so im working on a small side project tds type game, and i ran into a small bug.
The tower wont kill the mob.
the script:

local tower = script.Parent
local mobs = workspace.Mobs

local function FindNeartestTarget()
local maxDistance = 50
local nearestTarget = nil

for i, target in ipairs(mobs:GetChildren()) do
	local distance = (target.HumanoidRootPart - tower.Position).Magnitude
	print(target.Name, distance)
	if distance < maxDistance then
		print(target.Name, "is nearest target found so far")
		nearestTarget = target
		maxDistance = distance
		end
end

return nearestTarget
	
end

while true do
	local target = FindNeartestTarget()
	if target then
		target.Humanoid:TakeDamage(25)
		task.wait(1)
	end
end

Output:

you forgot to include the small wait in while loop, just replace the last few lines with this

while true do 	
    local target = FindNeartestTarget() 	
    if target then 		 
       target.Humanoid:TakeDamage(25) 		 
       task.wait(1) 	
    end 
    task.wait(0.05)
end

alr, thx for the help on that.
sadly there now another error:


imma go to sleep soon, because it is past 1 am for me

replace the 9th line with this (you forgot to put .Position after humanoidrootpart)

local distance = (target.HumanoidRootPart.Position - tower.Position).Magnitude
1 Like

thank you, your the best.
gn
charrrrr

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