Hello. I made a Custom Backpack, but, whenever a tool adds to the backpack and then I equip it again, it won’t tween the BackgroundTransparency and the BackgroundColor, also, whenever a tool removes from the backpack, it won’t update the backpack. Here is a video that shows what happens:
Here is my script:
local toOutput = true;
local frameFadeTime = 0.4;
local onEquipTransparency = 0;
local onUnequipTransparency = 0.2;
-- Defining variables.
local starterGui = game:GetService("StarterGui");
local coreGui = Enum.CoreGuiType.Backpack;
local players = game:GetService("Players");
local localPlayer = players.LocalPlayer;
local character = localPlayer.Character;
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 tools = {};
-- Disabling default backpack
local succed, stringError = pcall(function()
starterGui:SetCoreGuiEnabled(coreGui, false)
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 first_Load()
for ind, v in ipairs(backpack:GetChildren()) do
local clone = template:Clone();
clone.Parent = toolsHolder
clone.ToolName.Text = v.Name
clone.Index.Text = ind
local objValue = Instance.new("ObjectValue", clone);
objValue.Name = "ToolObject"
objValue.Value = v
table.insert(tools, #tools + 1, clone)
local isEqipped = Instance.new("BoolValue", clone)
isEqipped.Name = "IsEquipped"
local function mobile_button_Down()
local index = tonumber(clone.Index.Text)
if tools[index] then
if tools[index].IsEquipped.Value then
tools[index].IsEquipped.Value = false
tweenService:Create(tools[index], fadeTweenInfo, {BackgroundTransparency = 0.2, BackgroundColor3 = Color3.fromRGB(29, 29, 29)}):Play()
script.Unequip:FireServer()
else
tweenService:Create(tools[index], fadeTweenInfo, {BackgroundTransparency = 0, BackgroundColor3 = Color3.fromRGB(0, 170, 255)}):Play()
tools[index].IsEquipped.Value = true
script.Equip:FireServer(tools[index].ToolObject.Value)
for _, v in ipairs(tools) do
if v ~= tools[index] then
v.IsEquipped.Value = false
tweenService:Create(tools[index], fadeTweenInfo, {BackgroundTransparency = 0.2, BackgroundColor3 = Color3.fromRGB(29, 29, 29)}):Play()
end
end
end
elseif not tools[index] then
print("Index does not exist in the player's backpack.")
end
end
clone.Mobile.MouseButton1Down:Connect(mobile_button_Down)
end
end;
first_Load() -- Loading on the first time
function bacpack_Child_Added(obj)
local wasFound = false;
for _, v in ipairs(tools) do
if v.ToolObject.Value == obj then
wasFound = true
end
end
if wasFound then return end;
local clone = template:Clone();
clone.Name = tostring(#backpack:GetChildren())
clone.Parent = toolsHolder
clone.ToolName.Text = obj.Name
clone.Index.Text = #backpack:GetChildren()
local objValue = Instance.new("ObjectValue", clone);
objValue.Name = "ToolObject"
objValue.Value = obj
table.insert(tools, #tools + 1, clone)
local isEqipped = Instance.new("BoolValue", clone)
isEqipped.Name = "IsEquipped"
local function mobile_button_Down()
local index = tonumber(clone.Index.Text)
if tools[index] then
if tools[index].IsEquipped.Value then
tools[index].IsEquipped.Value = false
tweenService:Create(tools[index], fadeTweenInfo, {BackgroundTransparency = 0.2, BackgroundColor3 = Color3.fromRGB(29, 29, 29)}):Play()
script.Unequip:FireServer()
else
tweenService:Create(tools[index], fadeTweenInfo, {BackgroundTransparency = 0, BackgroundColor3 = Color3.fromRGB(0, 170, 255)}):Play()
tools[index].IsEquipped.Value = true
script.Equip:FireServer(tools[index].ToolObject.Value)
for _, v in ipairs(tools) do
if v ~= tools[index] then
v.IsEquipped.Value = false
tweenService:Create(tools[index], fadeTweenInfo, {BackgroundTransparency = 0.2, BackgroundColor3 = Color3.fromRGB(29, 29, 29)}):Play()
end
end
end
elseif not tools[index] then
print("Index does not exist in the player's backpack.")
end
end
clone.Mobile.MouseButton1Down:Connect(mobile_button_Down)
end;
function backpack_Child_Removed(obj)
if obj.Parent ~= localPlayer.Character then return end;
for ind, v in ipairs(tools) do
if v.ToolObject.Value == obj then
v:Destroy()
table.remove(tools, ind);
end
end
end;
function input_Began(input, gpe)
if gpe then return end;
if input.KeyCode == Enum.KeyCode.Backspace then
for ind, v in ipairs(tools) do
if v.IsEquipped.Value and v.ToolObject.Value.CanBeDropped then
table.find(tools, v):Destroy()
end
end
end
local index
if input.KeyCode == Enum.KeyCode.One then
index = 1
elseif input.KeyCode == Enum.KeyCode.Two then
index = 2
elseif input.KeyCode == Enum.KeyCode.Three then
index = 3
elseif input.KeyCode == Enum.KeyCode.Four then
index = 4
elseif input.KeyCode == Enum.KeyCode.Five then
index = 5
elseif input.KeyCode == Enum.KeyCode.Six then
index = 6
elseif input.KeyCode == Enum.KeyCode.Seven then
index = 7
elseif input.KeyCode == Enum.KeyCode.Eight then
index = 8
elseif input.KeyCode == Enum.KeyCode.Nine then
index = 9
end
if tools[index] then
if tools[index].IsEquipped.Value then
tools[index].IsEquipped.Value = false
tweenService:Create(tools[index], fadeTweenInfo, {BackgroundTransparency = 0.2, BackgroundColor3 = Color3.fromRGB(29, 29, 29)}):Play()
script.Unequip:FireServer()
else
tweenService:Create(tools[index], fadeTweenInfo, {BackgroundTransparency = 0, BackgroundColor3 = Color3.fromRGB(0, 170, 255)}):Play()
tools[index].IsEquipped.Value = true
script.Equip:FireServer(tools[index].ToolObject.Value)
for _, v in ipairs(tools) do
if v ~= tools[index] then
v.IsEquipped.Value = false
tweenService:Create(tools[index], fadeTweenInfo, {BackgroundTransparency = 0.2, BackgroundColor3 = Color3.fromRGB(29, 29, 29)}):Play()
end
end
end
elseif not tools[index] then
print("Index does not exist in the player's backpack.")
end
end;
backpack.ChildAdded:Connect(bacpack_Child_Added)
backpack.ChildRemoved:Connect(backpack_Child_Removed)
userInputService.InputBegan:Connect(input_Began)
Both functions backpack_Child_Added
and backpack_Child_Removed
handle it.