You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? Keep it simple and clear!
I want my tower to shoot the first enemy that enters its range -
What is the issue? Include screenshots / videos if possible!
My tower shoots at first enemy, but the fastest enemies when it enters the range of the tower, ends up having privileges and the tower attacks them instead of what is actually first
Gifs: https://gyazo.com/199226b919f22d08190d94fd734a90c0
- What solutions have you tried so far? Did you look for solutions on the Developer Hub? Yes, had a topic talking about, but did not talk much about how to fix this problem, so I ended up having more doubts, so I decided to create this topic
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
My Tower Script
-- local RunService = game:GetService("RunService")
local Enemys = workspace.Enemys
local EnemysTable = {}
local EnemysOrdener = {}
local HumanoidRootPart = script.Parent.HumanoidRootPart
local Torso = script.Parent.Torso
local Configuration = script.Parent.Configuration
local Path = workspace.Map.TesteMap.Path
function UpdateMatriz()
table.sort(EnemysOrdener, function(x,y)
return x[2] > y[2]
end)
end
function insert()
for i,v in pairs(Enemys:GetChildren())do
if v and v:IsA("Model") then
if EnemysOrdener[v] == nil and (HumanoidRootPart.Position - v.HumanoidRootPart.Position).magnitude <= Configuration.Scope.Value then
table.insert(EnemysOrdener, {v, v.Configuration.Distance.Value})
end
end
end
end
local function insertEnemys()
table.clear(EnemysOrdener)
EnemysTable = {}
insert()
UpdateMatriz()
print(EnemysOrdener)
for i,v in pairs(EnemysOrdener)do
if v and type(v) == "table" then
local enemySelected = v[1]
local Position = v[2]
local mag = (HumanoidRootPart.Position - enemySelected.HumanoidRootPart.Position).magnitude
if mag <= Configuration.Scope.Value then
table.insert(EnemysTable, enemySelected)
end
end
end
init()
end
function init()
if EnemysTable[1] == nil then return end
if Configuration.EnemyAttack.Value == "First" then
local EnemySelected = EnemysTable[1]
if EnemySelected ~= nil then
if EnemySelected:FindFirstChild("Humanoid") then
Torso.CFrame = CFrame.new(Torso.Position, Vector3.new(EnemySelected.HumanoidRootPart.Position.X, HumanoidRootPart.Position.Y, EnemySelected.HumanoidRootPart.Position.Z))
EnemySelected.Humanoid:TakeDamage(Configuration.Damage.Value)
if EnemySelected.Humanoid.Health <= 0 then
EnemySelected:Destroy()
EnemysOrdener[EnemySelected] = nil
EnemysTable[1] = nil
end
end
end
elseif Configuration.EnemyAttack.Value == "Last" then
local EnemySelected = EnemysTable[#EnemysTable]
if EnemySelected ~= nil then
if EnemysTable:FindFirstChild("Humanoid") then
HumanoidRootPart.AlignOrientation.Attachment1 = EnemySelected.HumanoidRootPart.Attachment
EnemySelected.Humanoid:TakeDamage(Configuration.Damage.Value)
end
end
end
wait(Configuration.Seconds.Value)
end
while true do
wait(0.1)
insertEnemys()
end
My Distance Script
local RunService = game:GetService("RunService")
local first = {}
local EnemysFolder = workspace.Enemys
local isRunning = false
EnemysFolder.ChildAdded:Connect(function(child)
if child:IsA('Model') then
first[child] = tick()
end
end)
RunService.Heartbeat:Connect(function(dt)
for i,v in pairs(EnemysFolder:GetChildren())do
if not v:IsA("Model") then continue end
coroutine.wrap(function()
local Speed = v.Humanoid.WalkSpeed - 0.5
v.Humanoid.Running:Connect(function(Run)
if Run >= Speed then
isRunning = true
else
isRunning = false
end
end)
if isRunning then
v.Configuration.Distance.Value += v.Humanoid.WalkSpeed * dt
v.Head.BillboardGui.TextLabel.Text = v.Configuration.Distance.Value
end
end)()
end
end)