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

How to shoot more than 1 ray in a raycast gun?

Asked by
Prioxis 673 Moderation Voter
8 years ago

So i'm making a shotgun using raycasting but I'm unsure on what I have to do to make it shoot multiple rays in the same direction

I have my script here

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local halt = 0
tool.Equipped:connect(function(mouse)
    print("Tool equipped!")

    mouse.Button1Down:connect(function()
        if halt == 1 then
            print'reloading'
        else
            halt = 1
        print("Mouse pressed!")
        local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 300)
        local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)

        local beam = Instance.new("Part", workspace)
        beam.BrickColor = BrickColor.new("Black")
        beam.FormFactor = "Custom"
        beam.Material = "Plastic"
        beam.Transparency = 0.25
        beam.Anchored = true
        beam.Locked = true
        beam.CanCollide = false

        local beam2 = Instance.new("Part", workspace)
        beam2.BrickColor = BrickColor.new("Black")
        beam2.FormFactor = "Custom"
        beam2.Material = "Plastic"
        beam2.Transparency = 0.25
        beam2.Anchored = true
        beam2.Locked = true
        beam2.CanCollide = false

    --  local distance = (tool.Handle.CFrame.p - position).magnitude
    local distance = 50

        beam2.Size = Vector3.new(0.3, 0.3, distance)
        beam2.CFrame = CFrame.new(tool.Handle.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)

        beam.Size = Vector3.new(0.3, 0.3, distance)
        beam.CFrame = CFrame.new(tool.Handle.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)

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

        if part then
            local humanoid = part.Parent:FindFirstChild("Humanoid")

            if not humanoid then
                humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
            end

            if humanoid then
                humanoid:TakeDamage(100)
                end
            end
            wait(10)
            halt = 0
        end
    end)
end)

beam2 is going to be my second ray and I believe on line 37 and 38 is where what I'm trying to accomplish will take place but I'm not 100% sure if that's correct..

1 answer

Log in to vote
0
Answered by
Kryddan 261 Moderation Voter
8 years ago

If I understood the question right then I think I got a way to do it.

Change it to a regular function and then add a for loop that runs the function multiple times.

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local halt = 0
tool.Equipped:connect(function(mouse)
    print("Tool equipped!")

    function fire()
        if halt == 1 then
            print'reloading'
        else
            halt = 1
        print("Mouse pressed!")
        local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 300)
        local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)

        local beam = Instance.new("Part", workspace)
        beam.BrickColor = BrickColor.new("Black")
        beam.FormFactor = "Custom"
        beam.Material = "Plastic"
        beam.Transparency = 0.25
        beam.Anchored = true
        beam.Locked = true
        beam.CanCollide = false

        local beam2 = Instance.new("Part", workspace)
        beam2.BrickColor = BrickColor.new("Black")
        beam2.FormFactor = "Custom"
        beam2.Material = "Plastic"
        beam2.Transparency = 0.25
        beam2.Anchored = true
        beam2.Locked = true
        beam2.CanCollide = false

    --  local distance = (tool.Handle.CFrame.p - position).magnitude
    local distance = 50

        beam2.Size = Vector3.new(0.3, 0.3, distance)
        beam2.CFrame = CFrame.new(tool.Handle.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)

        beam.Size = Vector3.new(0.3, 0.3, distance)
        beam.CFrame = CFrame.new(tool.Handle.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)

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

        if part then
            local humanoid = part.Parent:FindFirstChild("Humanoid")

            if not humanoid then
                humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
            end

            if humanoid then
                humanoid:TakeDamage(100)
                end
            end
            wait(10)
            halt = 0
        end
    end)

    mouse.Button1Down:connect(function()
            for i = 0,5 do
                   fire()
            end
    end

end)



0
No this isn't what I asked for I wanted it to be like a shotgun 4 bullets at once that shoot straight but spread out Prioxis 673 — 8y
Ad

Answer this question