Script has changed since I've made progress but it is still showing the same error.
local tower = script.Parent local mobs = workspace.Mobs
local function FindNearestTarget() local maxDistance = 50 local nearestTarget = nil
for i, target in ipairs(mobs:GetChildren()) do local distance = (target.HumanoidRootPart.Position - tower.Position).Magnitude print(target.Name, distance) if distance < maxDistance then print(target.Name, "is the nearest target found so far") nearestTarget = target maxDistance = distance end end return nearestTarget
end
while true do local target = FindNearestTarget() if target then target.Humanoid:TakeDamage(25) end
task.wait(1)
end
does anyone know how to fix it?
EDIT
for i, target in ipairs(mobs:GetChildren()) do if target ~= nil then if target:FindFirstChildOfClass("Humanoid") then local distance = (target.HumanoidRootPart.Position - tower.PrimaryPart.Position).Magnitude) print(target.Name, distance) if distance < maxDistance then print(target.Name, "is the nearest target found so far") nearestTarget = target maxDistance = distance end end return nearestTarget
Is my script supposed to look like this?
local tower = script.Parent local mobs = workspace.Mobs
local distance = (target.HumanoidRootPart.Position - tower.PrimaryPart.Position).Magnitude tower.PrimaryPart = tower:WaitForChild(PartName) print(distance)
local function FindNearestTarget() local maxDistance = 50 local nearestTarget = nil
for i, target in ipairs(mobs:GetChildren()) do if target ~= nil then if target:FindFirstChildOfClass("Humanoid") then local distance = (target.HumanoidRootPart.Position - tower.PrimaryPart.Position).Magnitude print(target.Name, distance)
if distance < maxDistance then print(target.Name, "is the nearest target found so far") nearestTarget = target maxDistance = distance end end
return nearestTarget