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

Why doesn't my recursive loop work as it should?

Asked by 5 years ago

I made a script that creates lasers and reflects them off of parts not in an ignore list. It works find in a part in workspace using a normal script, but when I try to implement it in a tool with a local script, it will just stop at the first point of impact. It seems that in the script, using a print with the count variable (print(count)), it will increment as many times as it should, but in the local script it only increments once every time you click. Why? I only modified the script slightly? Heres the code (I know its a lot I dont know where the problem is.):

Here is the normal script code in a part in workspace:

wait(5)

count = 0
maxCount = 10

ignoreList = game.Workspace.Glass:GetChildren()

table.insert(ignoreList, script.Parent)

function shoot(position, normal)
    local ray = Ray.new(position, normal * 500)
    local hit, endPosition, surfaceNormal = workspace:FindPartOnRayWithIgnoreList(ray, ignoreList)

    local beam = Instance.new("Part", workspace)    

    beam.BrickColor = BrickColor.new("Bright red")
    beam.FormFactor = "Custom"
    beam.Material = "Neon"
    beam.Transparency = 0.25
    beam.Anchored = true
    beam.Locked = true
    beam.CanCollide = false

    local distance = (position - endPosition).magnitude
    beam.Size = Vector3.new(0.3, 0.3, distance)
    beam.CFrame = CFrame.new(position, endPosition) * CFrame.new(0, 0, -distance / 2)

    if (hit) and count < maxCount then
        count = count + 1
        print(count)
        local reflectedNormal = (normal - (2 * normal:Dot(surfaceNormal) * surfaceNormal))
        local newNormal = reflectedNormal
        shoot(endPosition, newNormal)
    end
end

shoot(script.Parent.CFrame.p, script.Parent.CFrame.lookVector)

Here is the local script in a tool:

local tool = script.Parent.Parent
local player = game:GetService("Players").LocalPlayer
local canUse = true

count = 0
maxCount = 10

ignoreList = game.Workspace.Glass:GetChildren()

table.insert(ignoreList, script.Parent)
table.insert(ignoreList, script.Parent.Parent.Handle)

tool.Equipped:connect(function(mouse)

    mouse.Button1Down:connect(function()
        shoot(tool.Shoot.CFrame.p, (mouse.Hit.p - tool.Shoot.CFrame.p).unit)
    end)
end)

function shoot(position, normal)
    if canUse then
        canUse = false
        local ray = Ray.new(position, normal * 300)
        local hit, endPosition, surfaceNormal = workspace:FindPartOnRayWithIgnoreList(ray, ignoreList)

        local beam = Instance.new("Part", workspace)    

        beam.BrickColor = BrickColor.new("Bright red")
        beam.FormFactor = "Custom"
        beam.Material = "Neon"
        beam.Transparency = 0.25
        beam.Anchored = true
        beam.Locked = true
        beam.CanCollide = false

        local distance = (position - endPosition).magnitude
        beam.Size = Vector3.new(0.3, 0.3, distance)
        beam.CFrame = CFrame.new(position, endPosition) * CFrame.new(0, 0, -distance / 2)

        game:GetService("Debris"):AddItem(beam, 1)

        if (hit) and count < maxCount then
            count = count + 1
            print(count)
            local reflectedNormal = (normal - (2 * normal:Dot(surfaceNormal) * surfaceNormal))
            local newNormal = reflectedNormal
            shoot(endPosition, newNormal)
        end

        wait(1)

        canUse = true
    end
end
0
I figured it out, I just added a ray distance and max iterations parameter to the shoot script and moved count = count + 1 and some other miscellaneous stuff around. ronitrocket 120 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

I figured it out, I just added a ray distance and max iterations parameter to the shoot script and moved count = count + 1 and some other miscellaneous stuff around.

Ad

Answer this question