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 towers in this to be able to detect enemies near it in a radius -
What is the issue? Include screenshots / videos if possible!
the issue is that when any towers are placed, sometimes they just straight up dont detect enemies -
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
print statements, changing the conditional variables, and some other methods to preform the task clearly.
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!
script:
local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")
local part = script.Parent.HumanoidRootPart
local radius = 4
local nearestEnemy, nearestDistance = nil
local configuration = script.Parent:FindFirstChild("Configuration")
local dmg = configuration:FindFirstChild("DMG").Value
local cooldown = configuration:FindFirstChild("CD").Value
local function attack()
if not nearestEnemy then return end
print("passed")
local hum = nearestEnemy:FindFirstChild("Enemy")
print(nearestEnemy.Name.." enemy")
print(hum.Name.." humanoid")
if nearestEnemy and hum then
print("exists")
local punchAnim = script.Parent:FindFirstChild("PunchAnimation")
local anim = script.Parent.Humanoid.Animator:LoadAnimation(punchAnim)
anim:Play()
part.punch:Play()
if not hum then
print("humanoid doesnt exist")
return
end
print("Humanoid exists")
local enemyHp = hum.Health
hum:TakeDamage(dmg)
if hum.Health <= 0 then
nearestEnemy:Destroy()
nearestEnemy = nil
nearestDistance = 0
if game:GetService("Players"):FindFirstChild(script.Parent.PlacedBy.Value) then
game:GetService("Players"):FindFirstChild(script.Parent.PlacedBy.Value).leaderstats.Cash.Value += 20 + enemyHp
end
end
else
print("nearest enemy and hum do not not exist (can be either one)")
return
end
end
task.spawn(function()
while task.wait(cooldown) do
attack()
wait(cooldown)
end
end)
while task.wait() do
for _, enemy in pairs(workspace:FindFirstChild("Enemies"):GetChildren()) do
if not enemy then
return
end
local character = enemy
local distance = script.Parent.HumanoidRootPart.Position.Magnitude-enemy.HumanoidRootPart.Position.Magnitude
if not character or distance > radius or (nearestDistance and distance >= nearestDistance) then
continue
end
nearestDistance = distance
nearestEnemy = enemy
end
if nearestEnemy and nearestEnemy.PrimaryPart then
local targetPosition = Vector3.new(nearestEnemy.PrimaryPart.Position.X, part.Position.Y, nearestEnemy.PrimaryPart.Position.Z)
local look = CFrame.lookAt(part.Position, targetPosition)
for _,basePart:Part in pairs(script.Parent:GetChildren()) do
if basePart:IsA("Part") then
local tween = TweenService:Create(basePart, TweenInfo.new(0.05, Enum.EasingStyle.Quad, Enum.EasingDirection.In, 0, false, 0), {
CFrame = look * CFrame.Angles(0, math.rad(0), 0)
}):Play()
end
end
end
end
inside of the tower