Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Hotbar system doesn't, allow me to unequip by pressing same key?

I made my own Hotbar system, and it worked perfectly, but when I went to try to unequip by pressing the same key I used to equip I couldn't. Could someone help me track down, and solve this problem? Here's my script:

local Players = game:GetService("Players").LocalPlayer
local UI = script.Parent.HotbarFrame
local Keys = {
	["One"] = "1",
	["Two"] = "2",
	["Three"] = "3",
	["Four"] = "4",
	["Five"] = "5",
	["Six"] = "6",
	["Seven"] = "7",
	["Eight"] = "8",
	["Nine"] = "9",
}
local Tools = {}
local SlotSelected = 0

game:GetService("StarterGui"):SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)

function KeySelected(Slot)
	for i,v in pairs(UI:GetChildren()) do
		if v:IsA("ImageButton") then
			v.outline.Visible = false
			v.ToolName.TextColor3 = Color3.fromRGB(255, 255, 255)
		end
	end

	for i,v in pairs(Players.Character:GetChildren()) do
		if v:IsA("Tool") then
			v.Parent = Players.Backpack
		end
	end

	for i,v in pairs(Tools) do
		if not v:FindFirstChild("SlotName") then return end
		if v.SlotName.Value == Slot.Name then
			v.Parent = Players.Character
		end
	end
	SlotSelected = tonumber(Slot.SlotName.Text)
	Slot.outline.Visible = true
	Slot.ToolName.TextColor3 = Color3.fromRGB(255, 0, 0)
end

function ConvKeyCode(KeyCode)
	for i,v in pairs(Keys) do
		if KeyCode == Enum.KeyCode[i] then
			if UI:FindFirstChild("Slot"..v) then
				KeySelected(UI["Slot"..v])
			else
				print("Slot"..v, false)
			end
		end
	end
end

Players.Backpack.ChildAdded:Connect(function(Child)
	Tools = {}
	for i,v in pairs(Players.Backpack:GetChildren()) do
		if v:IsA("Tool") then
			table.insert(Tools, v)
			if not v:FindFirstChild("SlotName") then return end
			if UI:FindFirstChild(v.SlotName.Value)then
				UI:FindFirstChild(v.SlotName.Value).ToolName.Text = v.Name
			end
		end
	end
end)

game:GetService("UserInputService").InputBegan:Connect(function(Input)
	ConvKeyCode(Input.KeyCode)
end)
	

2 comments

0

Which part of your code is responsible for unequipping?

0

I didn't add, Could you help me add it. I do not know what I should do

1 answer

check if the tool with the number you pressed is equiped, if yes you can use that:

local Humanoid = Players:WaitForChild("Character"):WaitForChild("Humanoid")
Humanoid:UnequipTools()

This discussion is preserved as it was. New answers, comments, and votes are closed.