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

trying to make a automatic gun with Button1Down but for some reason button1 is always down?

so i am trying to make a automatic gun (never tryed making a automatic gun before) but when i try to make it where while Button1Down it shoots every 0.1 seconds it just shoots it constantly idk if im doing it wrong or i need to use another method if so then could you please point me in the right direction? here is my script

player = script.Parent.Parent.Parent -- the player
function fire(v)
	
	local bullet = game.ReplicatedStorage.bullet:Clone() -- clones a bullet from replicated storage
	bullet.Parent = workspace
	bullet.Velocity = v*1000
	bullet.Position = script.Parent.barrel.Position
	
	
	local force = Instance.new("BodyForce") -- makes new body force and puts in inside the bullet this way i can make the bullet drop
	force.force = Vector3.new(0,-5,0)
	force.Parent = bullet
	
	
		
	
	
end
------------- this is where im failing at
shooting = false

script.Parent.Unequipped:Connect(function()
	shooting = false
	
end)

script.Parent.Equipped:Connect(function(mouse)
	
	
	if mouse.Button1Down then
		print("down")
		shooting = true
	else 
		print("up")
		shooting = false
		
	end
	
	if shooting == true then
		local character = player.Character
		local targetpos = character.Humanoid.TargetPoint
		local lookat = (targetpos - character.Head.Position).unit
		fire(lookat)
		wait(0.1)
		print("fired")
	end
	
	end)	
	


1 answer

✓ Accepted answer

You want to do it like this:

player = script.Parent.Parent.Parent -- the player
function fire(v)

    local bullet = game.ReplicatedStorage.bullet:Clone() -- clones a bullet from replicated storage
    bullet.Parent = workspace
    bullet.Velocity = v*1000
    bullet.Position = script.Parent.barrel.Position


    local force = Instance.new("BodyForce") -- makes new body force and puts in inside the bullet this way i can make the bullet drop
    force.force = Vector3.new(0,-5,0)
    force.Parent = bullet





end
------------- this is where im failing at
shooting = false

script.Parent.Unequipped:Connect(function()
    shooting = false

end)

script.Parent.Equipped:Connect(function(mouse)
mouse.Button1Down:connect(function()
        print("down")
        local character = player.Character
        local targetpos = character.Humanoid.TargetPoint
        local lookat = (targetpos - character.Head.Position).unit
        fire(lookat)
        wait(0.1)
        print("fired")
end)
mouse.Button1Up:connect(function()
        print("up")
end)
end)    




Mouse.Button1Down and Mouse.Button1Up are events like Tool.Equipped is.

4 comments

0

thank you for you responce but the gun is still isnt automatic

0

nvm i modifyed it and it works now thanks

0

You're welcome :D

0

Use .Activated, it only fires when the player clicks with tool equipped.

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