UserInput Problem

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)


Sorry for bad grammar

I want fix this error
This error happend by uis still fire while i unequipped tools
308ce8809c493f424a5fd176c27f1158

its obviously cuz humanoid dosent exist in the player’s backpack

1 Like

Yeah i trying to fix problem that uis still fire while i unequipping tools

I’m still new to scripting as well but I noticed you did this

And should have done this.

local character = script.Parent.Parent
local hum = character:FindFirstChild("Humanoid")

it doesn’t help with your stated problems but I just think this will help in the future. :smiley:

1 Like

to get the players humanoid do

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
1 Like

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.

2 Likes

Thank you!!! You Solved my problem

1 Like