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

Press E to equip a tool And again for Unequip tool?

Asked by 6 years ago
local player = game:GetService("Players").LocalPlayer;
local mouse = player:GetMouse();
local tool = player.Backpack.StoneSword;
mouse.KeyDown:connect(function(key)
 if key == "q" then
  player.Character.Humanoid:EquipTool(tool);
 end
if key == "e" then
  player.Character.Humanoid:UnequipTool(tool); -- I have a problem with this part
 end
end);
1
I'm pretty sure that in the output there is an error stating that UnequipTool is not a valid member of Humanoid. You should state errors next time. F4ULT1NTH3D4T4 226 — 6y

1 answer

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
6 years ago
Edited 6 years ago

UnequipTool is not a function of the Humanoid object. Parent the tool to their Backpack.

Also, the KeyDown event is deprecated. Use InputBegan.

local player = game.Players.LocalPlayer;
local backpack = player:WaitForChild("Backpack");
local tool = backpack:WaitForChild("StoneSword");
local char = player.Character or player.CharacterAdded:Wait(); --Character
local hum = char:WaitForChild("Humanoid") --Humanoid
local uis = game:GetService("UserInputService") --service for InputBegan
local key = Enum.KeyCode.E --key to tap

uis.InputBegan:Connect(function(i,p) --'connect' is deprecated
    if p then return end --if they're typing do nothing
    if i.KeyCode == key then --If they've hit the correct key
        if not char:FindFirstChild(tool.Name) then --and tool is not equipped
            hum:EquipTool(tool); --equip it
        else --if tool is equipped
            tool.Parent = backpack; --unequip it.
        end
    end
end)
0
When i use it and then UnEquip it dont wil Eqip againe please help bolfstar 2 — 6y
0
Are you pressing the same key? I noticed your title stated "E" for equipping AND unequipping yet your code tried to use "Q" for equipping and "E" for unequipping Goulstem 8144 — 6y
0
No would like that if you press E that you have your weapon and if you press 1x E that it goes away but you can use your weapon bolfstar 2 — 6y
Ad

Answer this question