Hi, I trying to make block system on my weapon
But Userinputserivce have problem
i add tool equipped on uis
then when i dont equip tools it dont have problem but if i equip tools and unequip it and press the uis key
it still running like video
Here my script
script.Parent.Activated:Connect(function()
script.Parent.Attack:FireServer()
end)
local uis = game:GetService("UserInputService")
local hum = script.Parent.Parent:FindFirstChild("Humanoid")
local character = script.Parent.Parent
script.Parent.Equipped:Connect(function()
uis.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.F then
script.Parent.Block:FireServer()
end
end)
end)
script.Parent.Equipped:Connect(function()
uis.InputEnded:Connect(function(Input,Chatting)
if Chatting then
return
elseif Input.KeyCode == Enum.KeyCode.F then
script.Parent.Unblock:FireServer()
end
end)
end)
You have to be careful nesting bound events like that. They will repeatedly bind each time the tool is equipped, duplicating the logic. Connect() returns a RBXScriptConnection object which you can use to disconnect bindings to prevent this from happening.
local inputBeganConnection
local inputEndedConnection
tool.Equipped:Connect(function()
inputBeganConnection = uis.InputBegan:Connect(function(inputObject, gameProcessed)
end)
inputEndedConnection = uis.InputEnded:Connect(function(inputObject, gameProcessed)
end)
end)
tool.Unequipped:Connect(function()
-- clean up those binds when unequipped
if inputBeganConnection and inputBeganConnection.Connected then
inputBeganConnection.Disconnect()
end
if inputEndedConnection and inputEndedConnection.Connected then
inputEndedConnection.Disconnect()
end
end)
This will stop the input events from being handled while the tool is unequipped, as well as prevent overlapping the event logic when the tool is reequipped.