-
What do you want to achieve? Keep it simple and clear!
So, basically i need my tower shoot every enemy it see -
What is the issue? Include screenshots / videos if possible!
Issue is: After killing some enemies when every enemy leaves tower shoot zone tower goes to passive mode. When new wave is starting and enemies entering tower shoot zone, tower just do nothing.
– when enemies in tower shoot zone.
3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I was making that system by the youtube idk what’s wrong
My code:
local PhysicsService = game:GetService("PhysicsService")
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local events = ReplicatedStorage:WaitForChild("Events")
local SpawnTowerEvent = events:WaitForChild("SpawnTower")
local AnimateTowerEvent = events:WaitForChild("AnimateTower")
local tower = {}
local function FindNearestTarget(newTower)
local MaxDistance = 50
local nearestTarget = nil
for i, target in ipairs(workspace.MobsFolder:GetChildren()) do
local distance = (target.HumanoidRootPart.Position - newTower.HumanoidRootPart.Position).Magnitude
if distance < MaxDistance then
nearestTarget = target
MaxDistance = distance
end
end
return nearestTarget
end
function tower.Attack(newTower)
local target = FindNearestTarget(newTower)
if target then
print(target)
local TargetCFrame = CFrame.lookAt(newTower.HumanoidRootPart.Position, target.HumanoidRootPart.Position)
newTower.HumanoidRootPart.BodyGyro.CFrame = TargetCFrame
AnimateTowerEvent:FireAllClients(newTower, "Attack")
target:WaitForChild("Humanoid"):TakeDamage(25)
task.wait(1)
tower.Attack(newTower)
else
return
end
end