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?

32 comments
ok so I updated my script but is still showing the same error, do I need to do the same thing you said or is there a different way. Also it shows that Position is in the code and i'm not able to turn it into a primary part do you now why?
Yes you should do what i said.
If theres no primary part, the script will not work.
ok I will, put what should I change the script to.
also for some reason when I try to put the primary part into a part it cancels it right after I clicked it onto a part do you know how to fix it or any solutions?
Strange.. try setting the primary part via script
ok put where should I put the script since the changed script you send is no longer in the script
Oh no! keep the code in the script, just add the line where you set the primary part
ok I added back the code but now it's saying Workspace.Stone Tower.TowerTesting:4: attempt to index nil with 'Position' (to clarify Stone Tower is my tower that is gonna be used to attack the enimes and TowerTesting is the script name. Do you know how to fix this?
tower.PrimaryPart = tower:WaitForChild(PartName)
where do I put this line of code, because it's still showing the same error.
any
The error means that the code is trying to index something that does not exist.
ok I put it on line 4, and it shows some red text underneath some code, does that mean it doesn't work?
Can you send me the code?
I posted it in answers.
Does the tower have a humanoid?
no it doesn't.
Okay, make an invisible part inside of the tower, name it to "PrimaryPart" and change line 2 to local distance = (target.HumanoidRootPart.Position - tower.PrimaryPart.Position).Magnitude
ok I did and it says Workspace.Stone Tower.TowerTesting:4: attempt to index nil with "HumanoidRootPart' and the error is on line 2
also here is the youtube link for the tutorial I was on, maybe you can get some info to help me https://youtu.be/xSkSGO8rXUM
I've edited my comment, try that
do I delete the other stuff that isn't in your code?
Nah, you
You'll replace the code inside of the FindNearestTarget with mine
ok I updated my script so tell me if it's supposed to look like yours + some of mine.
How far have you made in the tutorial?
I've made it to like the beginning of trying to find the nearest target.
also it is showing this error but now I think we are getting closer and closer to fixing it! Workspace.Stone Tower.TowerTesting:26: Expected 'end' (to close 'then' at line 14), got ; did you forget to close 'then' at line 19? - Studio - TowerTesting:26
I'm going to go to sleep now so we can figure out the problem tomorrow, ok bye!
You should just redo the tutorial you know.
ok so i fixed the problem and now im on part 6 of the tutorial and im kind of stuck.