I am currently trying to make something like Tower Battles, I wonder how to make an tower. I’ve succeed to make it shoot and face the enemy, but it doesn’t shoot the first target. How I could make my tower shoot an first target in the range?
When a lot of enemys spawn, my towers get confused what to shoot, so thats why I want to make an tower to shoot first target.
Here is how it looks if someone wonders:
Here is script for shooting:
local Configuration = script.Parent.Parent.Configuration
function awardPlayers()
for i, v in pairs(game.Players:GetChildren()) do
if v:FindFirstChild("leaderstats") then
if v.leaderstats:FindFirstChild("Money") then
v.leaderstats.Money.Value = v.leaderstats.Money.Value + 1
end
end
end
end
while wait() do
local enemy = game.Workspace.Enemy:GetChildren()
for i = 1, #enemy do
if game.Workspace.Enemy:FindFirstChild(enemy[i].Name) then
if game.Workspace.Enemy[enemy[i].Name]:FindFirstChild("Humanoid") then
if game.Workspace.Enemy[enemy[i].Name]:FindFirstChild("HumanoidRootPart") then
if (game.Workspace.Enemy[enemy[i].Name].HumanoidRootPart.Position - script.Parent.Position).magnitude < Configuration.Range.Value then
if Configuration.Can.Value == true then
Configuration.Can.Value = false
game.Workspace.Enemy[enemy[i].Name].Humanoid:TakeDamage(Configuration.Damage.Value)
awardPlayers()
script.Parent.Parent.Output.FireSound:Play()
script.Parent.Parent.Output.ParticleEmitterFX.Enabled = true
script.Parent.Parent.Output.PointLightFX.Enabled = true
script.Parent.Parent.Output.SmokeFX.Enabled = true
wait(0.06)
script.Parent.Parent.Output.ParticleEmitterFX.Enabled = false
script.Parent.Parent.Output.PointLightFX.Enabled = false
script.Parent.Parent.Output.SmokeFX.Enabled = false
wait(Configuration.ReloadTime.Value)
Configuration.Can.Value = true
end
end
end
end
end
end
end
I want also to tell, it’s not best solution for shooting tower, but this what I could create and yes I know I should put
spawn(function())
in the particle and light effects instead of delaying rest of code/script.