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

Where is the delay in this script I found? [closed]

Asked by 6 years ago

I discovered a script for a kind of raycasting, and within it, there is some sort of "delay" for when the next laser is shot, after the previous laser has, but I can't figure out where it is, or how it was written. Help please?

while true do
    wait(0.1)
    function Shoot()

        local laser = Instance.new("Part")
        laser.Name = "Laser"
        laser.FormFactor = Enum.FormFactor.Custom
        laser.TopSurface, laser.BottomSurface = 0, 0
        laser.Size = Vector3.new(0.1, 0.1, 0.2)
        laser.BrickColor = BrickColor.Random()
        laser.Material = Enum.Material.Neon
        laser.Anchored = true
        laser.CanCollide = false
        laser.Locked = true
        laser.CFrame = script.Parent.CFrame
        laser.Parent = game.Workspace

        local maxDistance = 400
        local curDistance = 0

        local stepDistance = 4
        local stepWait = 0

        local currentPos = script.Parent.Position
        local currentNormal = script.Parent.CFrame.lookVector

        local function Step(overrideDistance)

            -- Cast ray:
            local ray = Ray.new(currentPos, currentNormal * (overrideDistance or stepDistance))
            local hit, pos, norm = game.Workspace:FindPartOnRayWithIgnoreList(ray, {script.Parent})

            -- Update laser position:
            laser.Size = Vector3.new(0.4, 0.4, (pos - currentPos).magnitude)
            laser.CFrame = CFrame.new(currentPos:lerp(pos, 0.5), pos)

            local oldPos = currentPos
            currentPos = pos

            if (hit) then
                -- r = d - 2(d DOT n)n
                -- Reflect:
                local reflect = (currentNormal - (2 * currentNormal:Dot(norm) * norm))
                currentNormal = reflect
                Step(stepDistance - (pos - oldPos).magnitude)
                return
            end

            curDistance = (curDistance + (pos - oldPos).magnitude)

            -- Apply fade effect to laser as it approaches max distance from < 75 studs:
            if (curDistance > (maxDistance - 75)) then
                local d = (curDistance - (maxDistance - 75)) / 75
                laser.Transparency = d
            end

            -- Recurse if max distance not reached:
            if (curDistance < maxDistance) then
                wait(stepWait)
                Step()
            end
        end

        Step()

        -- Done! Destroy laser:
        laser:Destroy()

    end

    Shoot() -- Fire shot!
end

Closed as Not Constructive by User#19524

This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.

Why was this question closed?