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

How to detect if a player is holding down a mouse button?

Asked by 7 years ago

Trying to create a gun and when they hold left mouse the gun will shoot continuously until they release the button.

1
scripting helpers is not a request site botw_legend 502 — 4y

3 answers

Log in to vote
1
Answered by
oSyM8V3N 429 Moderation Voter
7 years ago

You can use the InputBegan with the UserInputService to detect if the left mouse is still being held. An example is shown below :

local held = false -- creates a variable which we will use to see if the key is still being held
local timeheld = 0
local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(Input, gameProcessedEvent)
local KeyCode = Input.KeyCode
if not gameProcessedEvent then

if KeyCode == Enum.KeyCode.putyourkeyhere then
held = true

while held do -- since held = true, it runs a loop whiles the variable is set to true
wait()
timeheld = timeheld + 1 -- adds 1 to ``timeheld`` while held = true
print(timeheld)
end

end
end
end)

I haven't tested it yet, but if there's any errors please feel free to comment!.

--oSy

0
Sorry for the late acceptance answer and late thanks, but thanks! Mr_MilkysButler 47 — 6y
0
Np, its good oSyM8V3N 429 — 6y
Ad
Log in to vote
0
Answered by 7 years ago

I might have the answer, try this: (NOTE: The following script MUST be a LocalScript in StarterCharacterScripts. NAME the following script "MainGunScript")

local Mouse = game.Players.LocalPlayer:GetMouse() --The mouse.

local GunValue = script.FindFirstChild("GunValue")
GunValue.Value = nil

if not GunValue then
    GunValue = Instance.new("ObjectValue", script)
    GunValue.Name = "GunValue"
    GunValue.Value = nil
end

local ShootWait = script:FindFirstChild("ShootWait")
ShootWait.Value = 0

if not ShootWait then
    ShootWait = Instance.new("NumberValue", script)
    ShootWait.Name = "ShootWait"
    ShootWait.Value = 0 -- This is the short pause between every shot.
end

local Holding = false --This tells if the mouse button is holding or not.

Mouse.Button2Down:connect(function()
    Holding = true

    while Holding == true then

        if GunValue.Value ~= nil then

        --Here do the gun shooting thing, REFER THE GUN TOOL AS GunValue.Value:
        wait(ShootWait.Value)

        end

    end
end)

Mouse.Button2Up:connect(function() Holding = false end)

NOW, Put a LocalScript in the gun. Name it whatever you want. (I assume it's a tool, so put the following script IN the tool, not the handle.)

local MainGunScript = nil

script.Parent.Equipped:connect(function()
    MainGunScript = game.Players.LocalPlayer.Character:FindFirstChild("MainGunScript")

    if MainGunScript then
        MainGunScript:FindFirstChild("GunValue").Value = script.Parent
        MainGunScript:FindFirstChild("ShootWait").Value = 0.15 -- Set the short pause between shots in this one.
    end
end

script.Parent.Unequipped:connect(function()

    if MainGunScript then
        MainGunScript:FindFirstChild("GunValue").Value = nil
    end

end

Hope this helped! I made two whole scripts for you xD

0
The mouse is a working way to do it, and so far it's the easiest way to make a gun, but ROBLOX is looking to depreciate the mouse object soon. Eqicness 255 — 7y
Log in to vote
0
Answered by
RubenKan 3615 Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 years ago

Not sure why the other two overcomplicate it so much. you can just do

local MB = UserInputService:GetMouseButtonsPressed()
if MB.MouseButton1 then end -- left button down
if MB.MouseButton2 then end -- right button down
if MB.MouseButton3 then end -- scroll held down

http://wiki.roblox.com/index.php?title=API:Class/UserInputService/GetMouseButtonsPressed

Answer this question