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

defining .Touched function as a variable?

Asked by 6 years ago

So, I'm trying to make a sentry type thing that fires projectiles when it's "lane" is activated:

repeat wait() until script.Parent.base.Value ~= nil

local values = game.Workspace.values
local lanes = values.lanes
local myLane = lanes:FindFirstChild(script.Parent.base.Value)
local rs = game:GetService("RunService")


function fire()
    local projectile = Instance.new("Part")
    projectile.Size = Vector3.new(1,1,1)
    projectile.BrickColor = BrickColor.new("Dark Green")
    projectile.CanCollide = false
    projectile.Anchored = true
    projectile.CFrame = CFrame.new(script.Parent.head.Position.X + 3,script.Parent.head.Position.Y,script.Parent.head.Position.Z)
    projectile.Parent = script.Parent
    spawn(function()
        local projectileC = projectile
        local touch = projectileC.Touched:connect(function(t)end)
        repeat
            wait(.1)
            projectileC.Position = Vector3.new(projectileC.Position.X + 1,projectileC.Position.Y,projectileC.Position.Z)
        until touch
        print(touch)
    end)
end

while true do
    wait(1)
    if myLane.Value == true then
        fire()
    end
end

I know I could have the sentry and the projectiles run off of two different scripts to make things easier, but I don't want easy, I want to try to condense everything into one script to make the game run more efficiently. I'm attempting to do this by having a fire() function that creates a projectile when the sentries lane is activated and then having a spawn() function handle each projectiles movement. This works as it should, if i replace the code underneath the spawn() function with "while true do" and then the line that makes the projectile move, each projectile moves on its own as it should. However, i want the projectiles to move forward until they come into contact with another part. I'm attempting to do this by defining a touched() function as a variable and having the part repeat its movements until the touched function is fired. The problem is the part moves forward like 1-2 times before stopping in mid air (not touching anything, not even the part of the sentry it was fired from). I try to print the touch functions value to see what exactly it's touching, but all that gets printed to the output is "Connection". Any idea why this is happening? I'm assuming I'm misusing the touch function somehow, but I can't figure it out.

All help is appreciated!

Answer this question