Hello!
In short - I make my own toolbar with three slots for a specific tools. I’ll try to keep it as simple as possible.
The problem is that it works perfectly until I reset my character.
More specifically, “Backpack.ChildAdded” doesn’t work anymore after the reset.
Existing posts are not helpful. Also, this script is far from finished, and don’t ask me about strange solutions either, because I’m actually a modeler not a scripter.
Thanks!
local MainFrame = script.Parent
local Player = game.Players.LocalPlayer
local Backpack = Player.Backpack
local character = Player.Character
local humanoid = character:WaitForChild("Humanoid")
--Fix
local function FixSlots()
for i,v in pairs(MainFrame:GetChildren()) do
if v:IsA("TextButton") then
if Backpack:FindFirstChild(tostring(v.ToolObject.Value)) then
local Res = Backpack:FindFirstChild(tostring(v.ToolObject.Value))
v.ToolObject.Value = Res
end
end
end
end
Player.CharacterAdded:Connect(function()
character = Player.Character
humanoid = character:WaitForChild("Humanoid")
Player = game.Players.LocalPlayer
Backpack = Player:WaitForChild("Backpack")
MainFrame = script.Parent
FixSlots()
print("Current BP Parent:".. Backpack.Parent.Name )
end)
Backpack.ChildAdded:Connect(function(LastAdded) print("ChildAdded")
if MainFrame.SpecialToolFrame.ToolObject.Value ~= LastAdded and MainFrame.BottleFrame1.ToolObject.Value ~= LastAdded and MainFrame.BottleFrame2.ToolObject.Value ~= LastAdded then
if LastAdded:GetAttribute("Special") == true then
MainFrame.SpecialToolFrame.ToolObject.Value = LastAdded
MainFrame.SpecialToolFrame.Text = LastAdded.Name
elseif LastAdded:GetAttribute("Special") == false then
if MainFrame.BottleFrame1.ToolObject.Value == nil then
MainFrame.BottleFrame1.ToolObject.Value = LastAdded
MainFrame.BottleFrame1.Text = LastAdded.Name
else
MainFrame.BottleFrame2.ToolObject.Value = LastAdded
MainFrame.BottleFrame2.Text = LastAdded.Name
end
end
end
end)
local function CleanRemoved(LastRemoved)
if LastRemoved.Parent ~= character then
for i,v in pairs(MainFrame:GetChildren()) do
if v:IsA("TextButton") then
if v.ToolObject.Value == LastRemoved then
v.ToolObject.Value = nil
v.Text = "+"
end
end
end
end
end
Backpack.ChildRemoved:Connect(function(LastRemoved)
CleanRemoved(LastRemoved)
end)
--[[
character.ChildRemoved:Connect(function(LastRemoved)
if LastRemoved:IsA("Tool") then
CleanRemoved(LastRemoved)
end
end)
]]
Player.Character.ChildRemoved:Connect(function(LastRemovedIC)
if LastRemovedIC:IsA("Tool") then
if not Backpack:FindFirstChild(LastRemovedIC) then
for i,v in pairs(MainFrame:GetChildren()) do
if v:IsA("TextButton") then
if v.ToolObject.Value == LastRemovedIC then
v.ToolObject.Value = nil
v.Text = "+"
end
end
end
end
end
end)
local function UnEquipTools()
for i,v in pairs(character:GetChildren()) do
if v:IsA("Tool") then
v.Parent = Backpack
end
end
end
local function EquipTool(number)
--1
if number == 1 then
if MainFrame.SpecialToolFrame.ToolObject.Value ~= nil then
if MainFrame.SpecialToolFrame.ToolObject.Value.Parent == Backpack then
UnEquipTools()
MainFrame.SpecialToolFrame.ToolObject.Value.Parent = character
elseif MainFrame.SpecialToolFrame.ToolObject.Value.Parent == character then
MainFrame.SpecialToolFrame.ToolObject.Value.Parent = Backpack
UnEquipTools()
end
end
--2
elseif number == 2 then
if MainFrame.BottleFrame1.ToolObject.Value ~= nil then
if MainFrame.BottleFrame1.ToolObject.Value.Parent == Backpack then
UnEquipTools()
MainFrame.BottleFrame1.ToolObject.Value.Parent = character
elseif MainFrame.BottleFrame1.ToolObject.Value.Parent == character then
MainFrame.BottleFrame1.ToolObject.Value.Parent = Backpack
UnEquipTools()
end
end
--3
elseif number == 3 then
if MainFrame.BottleFrame2.ToolObject.Value ~= nil then
if MainFrame.BottleFrame2.ToolObject.Value.Parent == Backpack then
UnEquipTools()
MainFrame.BottleFrame2.ToolObject.Value.Parent = character
elseif MainFrame.BottleFrame2.ToolObject.Value.Parent == character then
MainFrame.BottleFrame2.ToolObject.Value.Parent = Backpack
UnEquipTools()
end
end
end
end
local UIP = game:GetService("UserInputService")
UIP.InputBegan:Connect(function(key)
if key.KeyCode == Enum.KeyCode.One then
EquipTool(1)
elseif key.KeyCode == Enum.KeyCode.Two then print("Key 2 pressed")
EquipTool(2)
elseif key.KeyCode == Enum.KeyCode.Three then
EquipTool(3)
end
end)