My script has doesn't work when its parent, a tower is placed. Script:
local me = game.Workspace.ExampleTower
local TS = game:GetService("TweenService")
repeat wait()
until script.Disabled == false
-- [VARIBLES]
local attackTime = 2 -- attacks every 5 seconds
local attackDmg = 2 -- attacks do 5 damage
local attackrange = 30 -- 40 stud attack range
function rotateYAxisToFace(originalPos, lookAtPos) return CFrame.new(originalPos, Vector3.new(lookAtPos.X, originalPos.Y, lookAtPos.z)) end
function checkParts()
local close = math.huge
local name
for _,v in pairs(game.Workspace:GetChildren()) do
print("checked:"..v.Name)
if v.Name == "Example Enemie" then
print(v.Name)
local dis = (me.Position - v.Position).magnitude
if dis < close then
close = dis
name = v
end
end
end
return name
end
function shoot(object)
--shoot
print("shot!")
local arrow = Instance.new("Part", me)
arrow.Anchored = true
arrow.Position = me.Position + Vector3.new(0,0,-1)
arrow.CanCollide = true
local goal = {}
goal.Position = object.Position
arrow.CFrame = CFrame.new(arrow.Position,object.position)
local TI = TweenInfo.new(0.1, Enum.EasingStyle.Linear)
local tween = TS:Create(arrow,TI,goal)
tween:play()
wait(0.1)
arrow:Destroy()
object.Health.Value = object.Health.Value - attackDmg
end
while true do
local en = checkParts()
me.CFrame = rotateYAxisToFace(me.Position,en.position)
local distance = (en.Position - me.Position).magnitude
if distance <= attackrange then
shoot(en)
end
wait(attackTime)
end

2 comments
il try that
did it work? i posted this a month ago.