Position is not a valid member of Model “Workspace.GrassLand.Mob.NormalZombie”
for i, target in pairs(mobs:GetChildren()) do
local distance = (target.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
HumanoidRootPart is not a valid member of Workspace "Workspace"
Right-- so you’re iterating a folder of models. Models do not have a Position property. You’d need to reference something within “target” being the PrimaryPart or HumanoidRootPart like I noted above.
HumanoidRootPart is not a valid member of Workspace "Workspace"
local tower = script.Parent.Parent
local mobs = game.Workspace.GrassLand.Mob
for i, target in ipairs(mobs:GetChildren()) do
local distance = (target.HumanoidRootPart.Position - tower.HumanoidRootPart.Position).Magnitude
Your error message seems irrelevant to this line of code, you’re not referencing Workspace here. Must be an error for somewhere else.
Is tower also a humanoid? You’re referencing its HumanoidRootPart as well. If tower is a model anmd target is a humanoid, it would look more like: local distance = (target.HumanoidRootPart.CFrame.Position - tower.PrimaryPart.CFrame.Position).Magnitude
And set the towers’ PrimaryPart to whatever base of it you want to use in determining distance.
This can’t be an error pertaining to the snippet of code we’re talking about though. What you’ve shared does not attempt to reference Workspace.HumanoidRootPart unless the heirarchy of where your “tower” is isn’t set up properly and script.Parent.Parent is Workspace. See that you’re referencing the right location there.