Tool not being equipped correctly on keypress

Hey there.

I have a script where you press the letter E and you get a tool added to the inventory and it automatically equips. However, the autoequip is kind of messed up and actually doesn’t properly equip it to the player’s hand until it’s reequipped by the player.

I couldn’t find a solution to my problem so I thought I’d post it here.

Code:

local UserInput = game:GetService("UserInputService")
local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()

local Tool = script["DH-17"]
local Clone = Tool:Clone()
UserInput.InputBegan:connect(function(Input)
    if Input.KeyCode == Enum.KeyCode.E then
		Clone.Parent = Character
    end
end)

Really, you don’t even need the Character instance at all, I do not think. You should set the clone’s parent to Player.Backpack instead of Character and remove anything that has to do with the character. Also, I believe that some tools do not work when added to the player’s backpack locally, though, so you may end up wanting to use a remote event to handle this server sided.

Let me know if this change works.

It would be better to use Humanoid:EquipTool(ToolObject) than cloning it into the Backpack or the character.

Edit: While using my backpack gui it works perfectly on the client and the server sees the same thing as on the client with Humanoid:EquipTool(). @Chatowillwin

When you clone tools into the player’s backpack, they are the tools that are crrently unequipped. The tools that are equipped are parented in the character instead. This means that you should first parent it to the backpack, then use the Humanoid:EquipTool() functions afterwards.

There’s also a couple other issues with your code so i’ll just fix them together.

local UserInputService = game:GetService("UserInputService")

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

local Tool = script:FindFirstChild("DH-17")

UserInputService .InputBegan:Connect(function(input, ignore)
    if ignore then return end
    if Input.KeyCode == Enum.KeyCode.E then
        if not Player.Backpack:FindFirstChild(Tool.Name) and not Character:FindFirstChild(Tool.Name) then
            local clonedTool = Tool:Clone()
            Humanoid:UnequipAllTools() -- make sure they aren't holding something already
            clonedTool.Parent = Player.Backpack
            Humanoid:EquipTool(clonedTool) -- equip it
        end
    end
end)

You forgot to use gameProcessedEvent (which I named ignore), and you used some deprecated functions like :connect() and wait(). Also, I made it so you clone it each time instead of just once, but then check if they have the tool already before parenting it.

1 Like

If tools are cloned by the client and the tool is controlled by a server script, the server script will not know that it has been parented to the player’s character/backpack, it wouldn’t notice any difference.

However, if there is a localscript controlling your tool, it should work just fine when cloning the tool on the client, but there’s always a catch: The server won’t have control over any of the tools you clone locally. Unless you keep a track of the player’s tools, that shouldn’t be a problem in general.

I would suggest that you use a RemoteEvent for the parenting part (if your tool is controlled by a server script).