This is a semi automatic gunfire script that works, however, I need to make it a fully automatic script, which means I need to loop it but I can't seem to figure out how to loop it correctly. What should I do to make it where the gunshots repeat until the mouse button is let up?
local MaxAmmo = 30 local Reloading = false local player = game.Players.LocalPlayer local Mouse = player:GetMouse() local playerGui = player:WaitForChild("PlayerGui") local TextLabel = playerGui:WaitForChild("AmmoDisplay"):FindFirstChild("AmmoText") local Ammo = MaxAmmo -- global reload function -- local function Reload() Reloading = true wait(1) Ammo = MaxAmmo Reloading = false end -- holding item, print equipped -- script.Parent.Equipped:Connect(function() print("G36 equipped") end) -- click to attack -- script.Parent.Activated:Connect(function() print("fired "..Ammo) if Ammo > 0 and not Reloading then Ammo -= 1 print(Ammo) script.Parent.GunShot:Play() workspace.Camera.CFrame = workspace.Camera.CFrame * CFrame.Angles(0.03,0,0) wait() workspace.Camera.CFrame = workspace.Camera.CFrame * CFrame.Angles(-0.01,0,0) wait() if Mouse.Target.Parent:FindFirstChild("Humanoid") then script.Parent.DealDamage:FireServer(Mouse.Target.Parent, 26) end elseif Reloading == false then Reload() script.Parent.GunShot:Stop() end while wait() do TextLabel.Text = Ammo.."/"..MaxAmmo end end) -- R to reload -- local Input = game:GetService("UserInputService") Input.InputBegan:Connect(function(Key) if Key.KeyCode == Enum.KeyCode.R and Reloading == false and Ammo ~= MaxAmmo then Reload() end end) -- deactivate detect -- script.Parent.Deactivated:Connect(function() print("Stopped Firing") end)
local Mouse= game.Players.LocalPlayer:GetMouse() local Equipped = false local IsMouseButtonDown = false Tool.Equipped:Connect(function() Equipped = true end) Tool.Unequipped:Connect(function() Equipped = false end) Mouse.Button1Down:Connect(function() IsMouseButtonDown = true while IsMouseButtonDown do -- code wait(.01) end end) Mouse.Button1Up:Connect(function() IsMouseButtonDown = false end)
Use Mouse
when the Tool
is equipped
local Mouse= game.Players.LocalPlayer:GetMouse() local Equipped = false Tool.Equipped:Connect(function() Equipped = true end) Tool.Unequipped:Connect(function() Equipped = false end) Mouse.Button1Down:Connect(function() -- Stuff end) Mouse.Button1Up:Connect(function() -- Stuff end)
Or you can use UserInputService
but you need to seperate it with other devices.
UserInputService.InputBegan:Connect(function(input, gameProcessed) if input.UserInputType == Enum.UserInputType.MouseButton1 then -- Stuff end end) UserInputService.InputEnded:Connect(function(input, gameProcessed) if input.UserInputType == Enum.UserInputType.MouseButton1 then -- Stuff end end) -- For mobile UserInputService.InputInput:Connect(function(input, gameProcessed) if input.UserInputType == Enum.UserInputType.Touchthen -- Stuff end end) UserInputService.InputEnded:Connect(function(input, gameProcessed) if input.UserInputType == Enum.UserInputType.Touchthen -- Stuff end end)
But make sure you use If statement
to check if the tool is equipped.
If it works tell me if not tell me.