Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make script run only when an object is placed? help?

Asked by 4 years ago

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 

1 answer

Log in to vote
0
Answered by 4 years ago

make the script disabled. then detect when the object is placed, when it is, make the script.Disabled = false, which enabled it.

--This is an example

script.Parent.Touched:Connect(function()
game.Workspace.Script.Disabled = false
end)
0
il try that marine5575 359 — 4y
0
did it work? i posted this a month ago. AlexanderYar 788 — 4y
Ad

Answer this question