I have a script that when an object, it will find the nearest enemy and shoot it, but when i try to place it, it doesn't run. the while loop is meant to hold up the script until it is placed, but after i place it, there is no farther output.
Script:
local me = script.Parent
local TS = game:GetService("TweenService")
while script.Enabled.Value == false do
wait()
print("Waiting!")
if script.Enabled.Value == true then
break
end
end
print("I've been enabled!")
-- [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
print("reeeeee")
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
because you need to break it
I did break it..?