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

How Do I Make it so the script only runs when the tool is equipped?

Asked by 2 years ago

Currently whenever I press e it runs the script but I only want it to run when the tool is equipped

local tool = script.Parent
local uis = game:GetService("UserInputService")
local rs = game:GetService("ReplicatedStorage")
local Nogr = rs.nogr

tool.Equipped:Connect(function()
    uis.InputBegan:Connect(function(input)
        if input.KeyCode == Enum.KeyCode.E then
         Nogr:FireServer()
        end
    end)    
end)

2 answers

Log in to vote
0
Answered by 2 years ago
local tool ,equipped= script.Parent,false
local uis = game:GetService("UserInputService")
local rs = game:FindService("ReplicatedStorage")
local Nogr = rs:WaitForChild"nogr"

tool.Equipped:Connect(function()equipped=true;end)
tool.Unequipped:Connect(function()equipped=false;end)
uis.InputBegan:Connect(function(input)if not equipped then return end
        if input.KeyCode == Enum.KeyCode.E then
         Nogr:FireServer()
        end
    end)    
0
It works thanks But do you mind explaining what you did mightydragon444 2 — 2y
Ad
Log in to vote
0
Answered by 2 years ago

im not the answer-er (lol) but i'll try to explain the code

local tool ,equipped= script.Parent,false --equipped is used as a debounce here
local uis = game:GetService("UserInputService") --getting userinputservice to detect input
local rs = game:FindService("ReplicatedStorage") --self explanatory
local Nogr = rs:WaitForChild"nogr" --self explanatory

tool.Equipped:Connect(function()equipped=true;end) --the variable equipped becomes true if the tool is equipped
tool.Unequipped:Connect(function()equipped=false;end) --the variable equipped becomes false if the tool is unequipped 
uis.InputBegan:Connect(function(input)if not equipped then return end --code ends if equipped is false
        if input.KeyCode == Enum.KeyCode.E then --if the user pressed 'E'
         Nogr:FireServer() --fires the remote event 
        end
    end)    

Answer this question