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

How do you modify this raycast to continuously fire?

Asked by 9 years ago

Hi there. I was curious to know how I could modify this wiki script to continuously fire. I was thinking about checking if the mouse is in the up position, and if not then stopping the laser, but that's beyond my capability.

Here's the script,

--Original script from the ROBLOX wiki, modified for this games purpose.

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

tool.Equipped:connect(function(mouse)
    mouse.Button1Down:connect(function()
        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("Bright red")
        --modify this to make the player choose what color they like!
        beam.FormFactor = "Custom"
        beam.Material = "Neon"
        beam.Transparency = 0.25
        beam.Anchored = true
        beam.Locked = true
        beam.CanCollide = false

        local distance = (tool.Handle.CFrame.p - position).magnitude
        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.25)

        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(1)
                wait(.1)
            end
        end
    end)
end)

1 answer

Log in to vote
0
Answered by 9 years ago

After awhile I was able to create a single beam rather than several projectiles.

Local Script

local tool = script.Parent
local toggle = tool:WaitForChild("Toggle")
local part = tool:WaitForChild("Handle")
local pos = tool:WaitForChild("pos")
local m
local u
local run = game:GetService("RunService").RenderStepped
local on = false
local equipped = true

local storepos = coroutine.create(function(mouse)
    while equipped do
        pos.Value = mouse.Hit.p
        run:wait()
    end
end)

tool.Equipped:connect(function()
    local player = game:GetService("Players").LocalPlayer
    repeat run:wait() until player.Character
    local character = player.Character
    local mouse = player:GetMouse()
    coroutine.resume(storepos,mouse)
    m = mouse.Button1Down:connect(function()
        on = true
        toggle:FireServer(true)
    end)
    u = mouse.Button1Up:connect(function()
        if not on then return end
        on = false
        toggle:FireServer(false)
    end)
end)

tool.Unequipped:connect(function()
    m:disconnect()
    u:disconnect()
    equipped = false
    if not on then return end
    on = false
    toggle:FireServer(false)
end)

Server Script

local tool = script.Parent
local toggle = script.Parent:WaitForChild("Toggle")
local part = script.Parent:WaitForChild("Handle")
local mousepos = script.Parent:WaitForChild("pos")
local on = false

local beam = Instance.new("Part")
beam.BrickColor = BrickColor.new("Really red")
beam.FormFactor = "Custom"
beam.Size = Vector3.new(0,0,0)
beam.TopSurface = "Smooth"
beam.BottomSurface = "Smooth"
beam.CanCollide = false
beam.Anchored = true
beam.Transparency = 0.5
beam.Material = "Neon"

toggle.OnServerEvent:connect(function(player,bool)
    if bool then
        on = true
        local character = player.Character
        local clone = beam:Clone()
        clone.Parent = tool
        while on do
            local pos = part.Position
            local ray = Ray.new(part.Position,(mousepos.Value - pos).unit*300)
            local hit,endpos = workspace:FindPartOnRay(ray,character,false,true)
            local distance = (pos - endpos).magnitude
            clone.Size = Vector3.new(0,0,distance)
            clone.CFrame = CFrame.new(pos, endpos)*CFrame.new(0,0,-distance/2)
            if hit then
                local human = hit.Parent:FindFirstChild("Humanoid")
                if human then
                    human:TakeDamage(1)
                end
            end
            wait()
        end
        clone:Destroy()
    else
        on = false
    end
end)

Note that these two scrips should be inside the tool. There should also be a remote event called "Toggle" and a Vector3Value called "pos" inside the tool.

This script would have been a lot easier had filtering enabled been switched off, but filtering enabled is important in keeping games secure.

The main problem posed by filtering enabled was creating the laser beam to be server-side instead of client-side, so that all players could see it. If that doesn't matter to you, then it's much easier to make the laser beam client-side.

To put this script simply, all I did was use the MouseButton1Down, MouseButton1Up, and Unequipped events to toggle whether the beam should be on or off, and I used a loop to manipulate the beam and check if that condition was true. If it wasn't, then I'd end the loop and delete the laser.

Seeing as it is you probably don't understand this script very well, I can't explain it to you very well until you learn the rest of the terminology. I made this script so you could use it as an example or guide to put this idea into perspective.

Good Luck!

Ad

Answer this question