What instantly came to mind for me when reading this is using a table, then using table.insert and table.remove every time a tool is added or removed, and using that table to organize which tool is 1, which is 2 and so on…
I assume what you want to achieve is having an organized set of tools set to specific numbers, so one tool is number 1, another is 2… This also applies to (“Oldest → Newest”).
By using table.remove the table should automatically re-position the variables so that 3 becomes 2 and 4 becomes 3, etc.
An example of adding a child to the table while it remains organized:
An example of removing a child from the table while it remains organized.
First I would have the localscript keep track of my tool table:
function ToolAdded(Tool)
if Tool:IsA("Tool") then
table.insert(ToolTable, Tool.Name)
end
end
function ToolRemoved(Tool)
if Tool:IsA("Tool") then
local Count = 0
for i,tools in pairs(ToolTable) do
Count = Count + 1
if tools.Name == Tool.Name then
table.remove(ToolTable, Count)
end
end
end
end
Backpack.ChildAdded:Connect(ToolAdded)
Backpack.ChildRemoved:Connect(ToolRemoved)
Note that I use the instances ChildAdded and ChildRemoved to detect each event, then adds/removes from the table accordingly.
Instance.ChildAdded
Instance.ChildRemoved
Now that we have the ToolTable constantly updated, we can keep track of UserInputService:
function KeyPressed(Key, gameProcessed)
if not gameProcessed then
if Key.KeyCode == Enum.KeyCode.One then
-- Equip table[1] FROM SERVER SIDE USING REMOTEEVENT
elseif Key.KeyCode == Enum.KeyCode.Two then
-- Equip Table[2] FROM SERVER SIDE USING REMOTEEVENT
end
end
end
InputService.InputBegan:Connect(KeyPressed)
Here I say that each KeyCode from UserInputService each equips a tool from the tool table on server side, there is probably a better way of doing this rather than defining each table child, however I assume there wouldn’t be too many.
Keep in mind you also have to parse through at least the tool as an argument so the server knows which tool to equip. You use the server side to equip the tools if you want it to replicate, otherwise it won’t.
The whole localscript:
Keep in mind this would need more work to actually function, however I am just going into the basics of how you could possibly do this.
repeat wait() until game.Players.LocalPlayer.Character
local InputService = game:GetService("UserInputService")
local PS = game:GetService("Players")
local LP = PS.LocalPlayer
local Backpack = LP:WaitForChild("Backpack")
local ToolTable = {}
function ToolAdded(Tool)
if Tool:IsA("Tool") then
table.insert(ToolTable, Tool.Name)
end
end
function ToolRemoved(Tool)
if Tool:IsA("Tool") then
local Count = 0
for i,tools in pairs(ToolTable) do
Count = Count + 1
if tools.Name == Tool.Name then
table.remove(ToolTable, Count)
end
end
end
end
function KeyPressed(Key, gameProcessed)
if not gameProcessed then
if Key.KeyCode == Enum.KeyCode.One then
-- Equip table[1] FROM SERVER SIDE USING REMOTEEVENT
elseif Key.KeyCode == Enum.KeyCode.Two then
-- Equip Table[2] FROM SERVER SIDE USING REMOTEEVENT
end
end
end
InputService.InputBegan:Connect(KeyPressed)
Backpack.ChildAdded:Connect(ToolAdded)
Backpack.ChildRemoved:Connect(ToolRemoved)
If you have any questions regarding my script, feel free to message me.