How do i make a simple tower script that uses magnitude but i want it to only target enemies with the lowest numbervalue and the high numbervalue?

  1. Pretty simple solution, a magnitude script with a damage script on it, so basically like when it finds a zombie in range from the ‘‘ZombieFolder’’ it will aim at it and deal damage to it, BUT… i also want to make it so it shoots the one with the Lowest NumberValue and the other one Being the Highest NumberValue.

  2. Issue is once all the time, I Don’t know the methods to code it.

  3. tables, but soon later got confused of it.

Anyways how to make table scripting more easier?

Do you have any “base” script?

alright wait sending one

ignore this

1 Like

this seems my best so far. i can change the ‘’ if distance < maxdistance then to if numbervalue is that and other is that lower or higher then shoot at it. i just need a way how to convert into that.

local tower = script.Parent
local mobs = workspace.Mobs
local humanoidnpc = script.Parent.Humanoid
local humanim = humanoidnpc:LoadAnimation(script.Parent.Shoot)
local function FindNearestTarget()
	local maxDistance = 60
	local nearestTarget = nil
	for i, target in ipairs(mobs:GetChildren()) do
	local distance = (target.HumanoidRootPart.Position - tower.HumanoidRootPart.Position).Magnitude
	if distance < maxDistance then
			tower:SetPrimaryPartCFrame(CFrame.new(tower.PrimaryPart.Position,target.Torso.Position))

		
		nearestTarget = target
		maxDistance = distance
	end
			
  end
 return nearestTarget	
	
end
while true do
	local target = FindNearestTarget()
	if target then
        humanim:Play()
		script.Parent.ShootSound:Play()
		target.Humanoid:TakeDamage(4)
        wait(1)
        script.Parent.Bolt:Play()
        
        
	end
	
	task.wait(4)
end
local function FindNearestTarget()
	local maxDistance = 60
	local nearestTarget = nil
	local oldTargetNumber = math.huge() --here
	local minimumTargetNumber = 1 --here
	for i, target in ipairs(mobs:GetChildren()) do
	local distance = (target.HumanoidRootPart.Position - tower.HumanoidRootPart.Position).Magnitude
	if distance < maxDistance then
		if target.TargetNumber.Value < oldTargetNumber and target.TargetNumber.Value > minimumTargetNumber then 
			tower:SetPrimaryPartCFrame(CFrame.new(tower.PrimaryPart.Position,target.Torso.Position))

		
			nearestTarget = target
			maxDistance = distance
		end
	end		
  end
 return nearestTarget

end

this might be helpful for lap numbers, but how do i make the math.huge to math.min? i also want to make a smallest number value in the script too!

I edited the code, use it and change the numbers to what you like,
Made a small mistake, fixed it now

alright ill try!

1

1 Like
table.sort(Enemies, function(Left, Right)
	return Left.Distance.Value < Right.Distance.Value and Left.Number.Value < Right.Number.Value
end)

This is just a pseudo-code example but should be simple enough to follow.

Does the Enemies has to be something special or should i leave as it a folder?