Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Add multiple event listeners to check if holding a tool?

Asked by 2 years ago
Edited 2 years ago

I am making a simple gun, and to make it automatically fire I have a function connected to a doing variable to check whether or not the mouse is down for automatic fire. However, I can now shoot even without the tool equipped because I have a button1down event instead of tool.Activated How do I add multiple events to this (A tool.Activated one to make sure they are holding the gun?)

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local M249shoot = game.ReplicatedStorage.GunFire
local BulletFolder = game.Workspace.Bullets

local debounce = false

local cooldown = 0.15
local doing = false

mouse.Button1Down:Connect(function()
    doing = true
    while doing do
        if debounce then return end
        debounce = true 

        local mousePosition = mouse.Hit.p
        local originPosition = origin.Position

        M249shoot:FireServer(mousePosition, originPosition)
        script.Parent.M249Sound:Play()

        wait(cooldown)
        debounce = false
    end
end)


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

1 answer

Log in to vote
0
Answered by 2 years ago

You just need to switch the events and the object in which the event resides in. You already have the tool referenced so you just switch mouse with M249shoot and switch the events

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local M249shoot = game.ReplicatedStorage.GunFire
local BulletFolder = game.Workspace.Bullets

local debounce = false

local cooldown = 0.15
local doing = false

M249shoot.Activated:Connect(function()
    doing = true
    while doing do
        if debounce then return end
        debounce = true 

        local mousePosition = mouse.Hit.p
        local originPosition = origin.Position

        M249shoot:FireServer(mousePosition, originPosition)
        script.Parent.M249Sound:Play()

        wait(cooldown)
        debounce = false
    end
end)


M249shoot.Deactivated:Connect(function()
    doing = false
end)
Ad

Answer this question