How can i improve this code

heres my code

local tool = script.Parent
local equipped =false
local uis = game:GetService(“UserInputService”)
local db = false
local dbtimer = 6
tool.RequiresHandle = false
– sets equipped to true when the tool is equipped and false when it is unequipped

tool.Equipped:Connect(function()

equipped = true

end)

tool.Unequipped:Connect(function()

equipped = false

end)

tool.Activated:Connect(function()

script.RemoteEvent:FireServer()

end)

game.UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if db == true then return end
if equipped == false then return end
if gameProcessedEvent == true then return end
if input.KeyCode == Enum.KeyCode.E then
script.Special:FireServer()
db = true
end
wait(dbtimer)
db = false
end)

local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")

local DEBOUNCE_TIMER = 6 -- constant

local localPlayer = Players.LocalPlayer
local tool = script.Parent
local debounce = false -- it's better to use full names (db -> debounce)

tool.RequiresHandle = false

tool.Activated:Connect(function()
	script.RemoteEvent:FireServer()
end)

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	local equipped = tool.Parent == localPlayer.Character -- instead of using 2 connections and tracking equip/unequip just check if the tool is in the character

	if gameProcessedEvent or debounce or not equipped or input.KeyCode ~= Enum.KeyCode.E then -- you can compress all of your checks into 1 if statement
		return
	end

	script.Special:FireServer()
	debounce = true

	task.wait(DEBOUNCE_TIMER) -- wait is deprecated, use task.wait() instead

	debounce = false
end)
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.