Trying to create a gun and when they hold left mouse the gun will shoot continuously until they release the button.
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
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
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