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

How would I modify this script to make work the mouse is held down instead of clicked?

Asked by 8 years ago

I'm trying to make an automatic laser weapon with a script based on the one found on the wiki, and I want it to fire as long as the mouse button is held down.

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

tool.Equipped:connect(function(mouse)
    print("Tool equipped!")

    mouse.Button1Down:connect(function()
        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("Bright red")
        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.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(30)
            end
        end
    end)
end)
local tool = script.Parent
local player = game:GetService("Players").LocalPlayer

tool.Equipped:connect(function(mouse)
    print("Tool equipped!")

    mouse.Button1Down:connect(function()
        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("Bright red")
        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.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(30)
            end
        end
    end)
end)

1 answer

Log in to vote
1
Answered by 8 years ago

Looping, and event triggers

I'm not going to touch your code, but here's some good example code that you will understand.

local Mouse = game.Players.LocalPlayer:GetMouse();
local down = false;
Mouse.Button1Down:connect(function()
down = true;
while down do
print("DOWN");
wait();
end;
end);
Mouse.Button1Up:connect(function()
down = false;
end);

woof woof
0
Would be better if your code snippet was indented correctly. XAXA 1569 — 8y
0
Yes well there's only so much you can do with paws and indentation is not one of them. CailThePuppy 70 — 8y
0
after around five or six studio crashes i finally got it to work. thanks dude TehPootisman_DxD 44 — 8y
Ad

Answer this question