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

How to stop spamming equip to increase firerate?

With my gun, when you spam equip and unequip, it makes it shoot way faster than intended. Here is my code:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local tool = script.Parent
local equipped = false

local mouseDown = false
local canPlay = true
local sound = script.Parent.shoot
local mouse = game.Players.LocalPlayer:GetMouse()


mouse.Button1Down:Connect(function()
	mouseDown = true
end)

mouse.Button1Up:Connect(function()
	mouseDown = false
end)

tool.Equipped:Connect(function()
	equipped = true
end)

tool.Unequipped:Connect(function()
	equipped = false
end)

tool.Equipped:Connect(function()
	
	while wait(0.2) do
		if mouseDown then
			if equipped then
				if canPlay then
					canPlay = false
					
					local ray = Ray.new(tool.shootPart.CFrame.p, (mouse.Hit.p - tool.shootPart.CFrame.p).unit * 300)
					local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
					
					local beam = Instance.new("Part", workspace)
					beam.BrickColor = BrickColor.new("Brick yellow")
					beam.FormFactor = "Custom"
					beam.Material = "Neon"
					beam.Transparency = 0.5
					beam.Anchored = true
					beam.Locked = true
					beam.CanCollide = false
					
					local distance = (tool.shootPart.CFrame.p - position).magnitude
					beam.Size = Vector3.new(0.1, 0.1, distance)
					beam.CFrame = CFrame.new(tool.shootPart.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
					
					game:GetService("Debris"):AddItem(beam, 0.1)
					
					if part then
						local humanoid = part.Parent:FindFirstChild("Humanoid")
						
						if not humanoid then
							humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
						end
						
						if humanoid then
							humanoid:TakeDamage(49)
						end
					end
					
					sound:Play()
					
					
					canPlay = true
				end
			end
		end
	end
end)

Fairly new scripter so any help is appreciated

1 answer

try if this works

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local tool = script.Parent
local equipped = false
local bool = Instance.new("BoolValue",player)
bool.Value = false
local mouseDown = false
local canPlay = true
local sound = script.Parent.shoot
local mouse = game.Players.LocalPlayer:GetMouse()


mouse.Button1Down:Connect(function()
    mouseDown = true
end)

mouse.Button1Up:Connect(function()
    mouseDown = false
end)

tool.Equipped:Connect(function()
    equipped = true
end)

tool.Unequipped:Connect(function()
    equipped = false
end)

tool.Equipped:Connect(function()

    while wait(0.2) do
        if mouseDown then
            if equipped then
                if canPlay then
                    canPlay = false
                    if bool == false then
	                   bool = true
                    local ray = Ray.new(tool.shootPart.CFrame.p, (mouse.Hit.p - tool.shootPart.CFrame.p).unit * 300)
                    local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)

                    local beam = Instance.new("Part", workspace)
                    beam.BrickColor = BrickColor.new("Brick yellow")
                    beam.FormFactor = "Custom"
                    beam.Material = "Neon"
                    beam.Transparency = 0.5
                    beam.Anchored = true
                    beam.Locked = true
                    beam.CanCollide = false

                    local distance = (tool.shootPart.CFrame.p - position).magnitude
                    beam.Size = Vector3.new(0.1, 0.1, distance)
                    beam.CFrame = CFrame.new(tool.shootPart.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)

                    game:GetService("Debris"):AddItem(beam, 0.1)

                    if part then
                        local humanoid = part.Parent:FindFirstChild("Humanoid")

                        if not humanoid then
                            humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
                        end

                        if humanoid then
                            humanoid:TakeDamage(49)
                        end
                    end

                    sound:Play()

                    bool.Value = true
                    canPlay = true
end
                end
            end
        end
    end
end)

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