I am trying to make a first targetting tower script and somehow this is what it was suppose to make first targetting, so i usually wanted the ‘‘DistanceToWaypoint’’ to be the lowest and ‘‘MovingValue’’ To be the highest, so its like if DisToWaypoint is lower and MovingValue here is the highest for the zombie then it will shoot that, but somehow it still shoots randomly.

while wait(0.3) do
local towertorso = script.Parent.Torso
local mobs = workspace.Enemies
local humanoid = script.Parent.Humanoid
local humanoidnpc = script.Parent.Humanoid
--local humanim = humanoidnpc:LoadAnimation(script.Parent.fire)
local function FindFirstTarget()
local maxDistance = 150
local nearestTarget = nil
for i, target in ipairs(mobs:GetChildren()) do
local distance = (target.HumanoidRootPart.Position - towertorso.Position).Magnitude
if target.DistanceToWaypoint.Value < 999 and target.MovingValue.Value > 0 then
towertorso.Parent:SetPrimaryPartCFrame(CFrame.new(towertorso.Parent.PrimaryPart.Position,target.Torso.Position))
maxDistance = distance
end
end
end
while true do
local target = FindFirstTarget()
local FireSound = Instance.new('Sound')
local damagenumber = 3
if target then
-- Do something
end
task.wait(0.5)
end
end
Any solutions?
What exactly is this DistanceToWaypoint and how does it work? is it a value or an attribute?
its a value thats being changed by a magnitude script showing how close you are the to next waypoint.
Got it
I don’t think that is the best solution to this since, in certain points of the map, the mobs may be closer to a past waypoint than to the next one. this might be the issue you are having. i don’t really know how to go about solving this either, but i would suggest you take a look at the Dev Hub
Good Luck with your game from now on and sorry for not being able to help any further since i myself am not that experienced lol
You will need 2 NumberValues in the script.
1 is named TargetDistance
The other one is named TargetDistance
script.NearestTarget.Value = nil
script.TargetDistance.Value =999999--put the max shooting distance here
local towertorso = script.Parent.Torso
local mobs = workspace.Enemies
local humanoid = script.Parent.Humanoid
local humanoidnpc = script.Parent.Humanoid
--local humanim = humanoidnpc:LoadAnimation(script.Parent.fire)
while true do
--gets closest Enemy
for i, target in ipairs(mobs:GetChildren()) do
local distance = (target.HumanoidRootPart.Position - towertorso.Position).Magnitude
if distance < script.TargetDistance.Value then
script.TargetDistance.Value = distance
script.NearestTarget.Value = target
end
end
--------------Done getting Enemy
local FireSound = Instance.new('Sound')
local damagenumber = 3
if target then
-- Do something
end
task.wait(0.5)
end
your post kinda confuses me
2 numbervaules being named TargetDistance???
plus target now gets a orange line under, meaning it has nothing.
Sorry, 1 of them needs to be TargetDistance, the other should be NearestTarget. NearestTarget should be an object value.
if target then| should be| if script.NearestTarget.Value ~= nil then
Yeah but i wanted like if this target has a higher value in that and a smaller value in that, then choose the zombie as the target somehow i can’t see its in the script.
and also somehow, it does print target, but it prints all the zombies but somehow doesn’t print more.
So you want If the zombie has a higher “Rank” then choose it instead?
And It prints all the Zombies? You want only 1 right?
Yeah like something like if zombie has lowest magnitude numbervalue and has highest waypoint numbervalue then choose that zombie if its range.
Which is more important, The Zombie the farthest along the path(waypoint) or the closer one?
well the magnitude script just shows how close is the zombie to it, and the waypoint numbervalue shows what waypoint the zombie is currently going to.

The “T” is the tower, The 2 “Z’s” are the zombies. The first one one is closer, but the 2nd one is closer to the end of the path. Which one Should be Targeted?
The farthest, and that doesn’t coding for the zombie, the farthest zombie probably has Waypoint value to 10 and 35 studs to the waypoint.
So which ever one is closer to the end or “base” should be targeted. Ignore the close one?
You will need to add an ObjectValue called FarthestTargetAlongPath in the script
script.TargetDistance.Value = 999999
script.FarthestTargetAlongPath.Value.Value = nil
local towertorso = script.Parent.Torso
local mobs = workspace.Enemies
local humanoid = script.Parent.Humanoid
local humanoidnpc = script.Parent.Humanoid
--local humanim = humanoidnpc:LoadAnimation(script.Parent.fire)
while true do
--gets closest Enemy
for i, target in ipairs(mobs:GetChildren()) do
local distance = (target.HumanoidRootPart.Position - towertorso.Position).Magnitude
if distance < 9999--max distance here and target.CurrentWaypoint.Value > script.FarthestTargetAlongPath.Value then
script.TargetDistance.Value = distance
script.FarthestTargetAlongPath.Value = target
end
end
--------------Done getting Enemy
local FireSound = Instance.new('Sound')
local damagenumber = 3
if script.FarthestTargetAlongPath.Value ~= nil then
-- Do something
end
task.wait(0.5)
end
why does FarthestTargetAlongPath has 2 values in the second line?