Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

my automatic gun script isnt working but there is errors, why?

Im making an ak47 for my game and im trying to make it so you can hold down instead of just spam clicking, the code doesnt work but i dont get any errors. this is the code

local mousedown = false
local uis = game:GetService("UserInputService")
script.Parent.Equipped:Connect(function(mouse)
		uis.InputBegan:Connect(function(Input)
			if Input == Enum.UserInputType.MouseButton1 then
				mousedown = true
			end
			uis.InputEnded:Connect(function(Input)
				mousedown = false
		end)
	end)
	while mousedown do
		wait(0.5)
		game.ReplicatedStorage.GunEvent2:FireServer(script.Parent.Barrel.Flash.Position, mouse.Hit.Position)
	end
end)

1 comment

0

if theres no errors that means the script is doing what it is told im not sure bit i belive this is a bug

1 answer

Here's how I do it. Instead of using user input service, I use tool activated with a repeat.

local firing = false
equipped = false

script.Parent.Equipped:Connect(function()
	equipped = true
end)
script.Parent.Unequipped:Connect(function()
	equipped = false
end)

script.Parent.Activated:Connect(function()
	firing = true
	repeat
		if equipped == true then
	
			game.ReplicatedStorage.GunEvent2:FireServer(script.Parent.Barrel.Flash.CFrame.Position, mouse.Hit.p)
	
			
			wait(--//Place Cool Down here. Make it something like 60/700, makes the gun fire 700 rounds per minute, don't touch the 60)
		end
	until firing == false 
end)
Tool.Deactivated:Connect(function()
	if firing == true then
		firing = false
	end
end)

Let me know if there are any errors. Cheers, Zander.

This discussion is preserved as it was. New answers, comments, and votes are closed.