Hello. I made a custom backpack to share for others as a free source, but for some reason, the unequipping of the tool won’t work.
I’ll press the correct key, but it won’t work. If there are multiple tools and certain tool is equipped and I press a key of another tools, it unequips the existing one, and equips the new one. Here is my code:
--// Created by XDvvvDX.
-- Customizing options (More coming soon, make sure to recommend in the post!)
local toOutput = false;
local frameFadeTime = 0.4;
local onEquipTransparency = 0;
-- Defining variables.
local starterGui = game:GetService("StarterGui");
local coreGui = Enum.CoreGuiType.Backpack;
local changeValue = false;
local players = game:GetService("Players");
local localPlayer = players.LocalPlayer;
local backpack = localPlayer.Backpack;
local toolsHolder = script.Parent.ToolsHolder;
local template = script.Template;
local userInputService = game:GetService("UserInputService");
local tweenService = game:GetService("TweenService");
local fadeTweenInfo = TweenInfo.new(frameFadeTime);
local isToolEquipped = false;
-- Disabling default backpack
local succed, stringError = pcall(function()
starterGui:SetCoreGuiEnabled(coreGui, changeValue)
end)
repeat wait() until succed or stringError
if succed then
if toOutput then
print("Succesfully disabled default backpack.")
end
else
if toOutput then
warn("An error has occured while disabling default backpack.")
end
end
function load_Backpack()
for ind, v in ipairs(backpack:GetChildren()) do -- Looping through the player's backpack.
if v:IsA("Tool") then
local templateClone = template:Clone(); -- Cloning the existing template.
templateClone.Index.Text = tostring(ind);
templateClone.ToolName.Text = string.upper(tostring(v.Name));
templateClone.Parent = toolsHolder;
end
end
end;
load_Backpack() -- If a player resetes i.e just join, it will make sure to load his current backpack.
function remove_Existing_Backpack()
for _, v in ipairs(toolsHolder:GetChildren()) do -- Looping through the tools holder frame.
if v:IsA("Frame") then
v:Destroy() -- Destroying the instance if it is a frame.
else
end
end
end;
function update_Backpack()
remove_Existing_Backpack() -- Removing the current existing backpack.
for ind, v in ipairs(backpack:GetChildren()) do -- Looping through the player's backpack.
if v:IsA("Tool") then
local templateClone = template:Clone(); -- Cloning the existing template.
templateClone.Index.Text = tostring(ind);
templateClone.ToolName.Text = string.upper(tostring(v.Name));
templateClone.Parent = toolsHolder;
end
end
end;
function convert_KeyCode_To_Number(keyCode)
if keyCode == Enum.KeyCode.One then
return 1
elseif keyCode == Enum.KeyCode.Two then
return 2
elseif keyCode == Enum.KeyCode.Three then
return 3
elseif keyCode == Enum.KeyCode.Four then
return 4
elseif keyCode == Enum.KeyCode.Five then
return 5
elseif keyCode == Enum.KeyCode.Six then
return 6
end
end;
function child_Added(instance)
if not instance:IsA("Tool") then return end;
update_Backpack() -- Whenever an instance adds inside the player's backpack, it will update his current backpack.
end;
function child_Removed(instance)
if not instance:IsA("Tool") then return end;
update_Backpack() -- Whenever an instance removes inside the player's backpack, it will update his current backpack.
end
function child_Changed(instance)
if not instance:IsA("Tool") then return end;
update_Backpack() -- Whenever an instance changes inside the player's backpack [name i.e], it will update his current backpack.
end
function input_Began(input, isTyping)
if isTyping then return end; -- Not to fire if the player was typing while pressing the key.
local keys = {
Enum.KeyCode.One;
Enum.KeyCode.Two;
Enum.KeyCode.Three;
Enum.KeyCode.Four;
Enum.KeyCode.Five;
Enum.KeyCode.Six;
Enum.KeyCode.Seven;
}; -- Defining each key
local wasFound = false;
local wasIndexToolFound = false;
local indexKeyPresed = convert_KeyCode_To_Number(input.KeyCode)
local toolObject = nil;
for _, v in ipairs(keys) do
if input.KeyCode == v then
wasFound = true
else
end
end
if not wasFound then
return
else
if toOutput then
print("Correct key passed.")
end
for ind, v in ipairs(backpack:GetChildren()) do
if ind == indexKeyPresed then
wasIndexToolFound = true
toolObject = v
else
end
end
if not wasIndexToolFound then return end;
if not isToolEquipped then
local remoteEvent = script.Equip;
remoteEvent:FireServer(toolObject)
isToolEquipped = true
for ind, v in ipairs(toolsHolder:GetChildren()) do
if not v:IsA("UIListLayout") then
if ind - 1 == indexKeyPresed then -- Index - 1 as UIGridLayout includes as well.
tweenService:Create(v, fadeTweenInfo, {
BackgroundTransparency = onEquipTransparency;
}):Play()
end
end
end
else
local equipObject = nil;
for ind, v in ipairs(backpack:GetChildren()) do
if ind == indexKeyPresed then
equipObject = v
end
end
if equipObject == nil then
local remoteEvent = script.Unequip;
remoteEvent:FireServer()
isToolEquipped = false
for _, v in ipairs(toolsHolder:GetChildren()) do
if v:IsA("Frame") and v.BackgroundTransparency ~= 0.2 then
tweenService:Create(v, fadeTweenInfo, {
BackgroundTransparency = 0.2;
}):Play()
end
end
elseif equipObject ~= nil then
local toolToClone = backpack:GetChildren()[indexKeyPresed];
local remoteEvent = script.Unequip;
remoteEvent:FireServer()
for _, v in ipairs(toolsHolder:GetChildren()) do
if v:IsA("Frame") and v.BackgroundTransparency ~= 0.2 then
tweenService:Create(v, fadeTweenInfo, {
BackgroundTransparency = 0.2;
}):Play()
end
end
local remoteEvent2 = script.Equip
remoteEvent2:FireServer(toolObject)
isToolEquipped = true
for ind, v in ipairs(toolsHolder:GetChildren()) do
if not v:IsA("UIListLayout") then
if ind - 1 == indexKeyPresed then -- Index - 1 as UIGridLayout includes as well.
tweenService:Create(v, fadeTweenInfo, {
BackgroundTransparency = onEquipTransparency;
}):Play()
end
end
end
end
end
end
end
-- Events connecting
userInputService.InputBegan:Connect(input_Began)
backpack.ChildAdded:Connect(child_Added)
backpack.ChildRemoved:Connect(child_Removed)
The code that handles it is in function input_Began()