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

How could I change this to allow for specific item{s} first?

Below is a re-moulded version of Shrio's custom backpack; It works perfectly in-game: However, I'm looking to add a specific item first in the list-layout when displayed.

To elaborate on this, Player's collect stuff as per usual - However; whenever a specific tool is added into the Player's Backpack { I.E. "Player1's Wand"; "Wand", etc. } it is ALWAYS the first in the list and if any others were in it's place prior then they are pushed up by 1.

I believe the snippet below to involve the necessary parts to adjust this. If not I have included the entire script also.

SNIPPET:

function ChildAdded(Child)
		if Child:IsA("Tool") then
			local isNew = true
			for _, Value in pairs(inputKeys) do
				local Tool = Value["Tool"]
				if Tool then
					if Tool == Child then
						isNew = false
					end
				end
			end
			if isNew == true then
				for i = 1, #inputOrder do wait()
					local Tool = inputOrder[i]["Tool"]
					if not Tool then
						inputOrder[i]["Tool"] = Child
						break
					end
				end
			end
			Configure()
		end
	end

local System = {}

local UserInputService = game:GetService("UserInputService")

local CoreGui = script.Parent.Parent
local Gui = CoreGui["ToolBar"]

local Player = game:GetService("Players").LocalPlayer

local inputKeys =
{["One"] = {Text = "1"};
 ["Two"] = {Text = "2"};
 ["Three"] = {Text = "3"};
 ["Four"] = {Text = "4"};
 ["Five"] = {Text = "5"}}

local inputOrder =
{inputKeys["One"]; inputKeys["Two"]; inputKeys["Three"]; inputKeys["Four"]; inputKeys["Five"]}

function System:Enable()
	local PlayerTools = Player["Backpack"]:GetChildren()
	for i = 1, #PlayerTools do wait()
		if PlayerTools[i]:IsA("Tool") then
			for i = 1, #inputOrder do wait()
				local Value = inputOrder[i]
				if not Value["Tool"] then
					Value["Tool"] = PlayerTools[i]
					break
				end
			end
		end
	end
	Create()
	UserInputService.InputBegan:Connect(onKeyPress)
	Player.Character.ChildAdded:Connect(ChildAdded)
	Player.Character.ChildRemoved:Connect(ChildRemoved)
	Player["Backpack"].ChildAdded:Connect(ChildAdded)
	Player["Backpack"].ChildRemoved:Connect(ChildRemoved)
end

function Create()
	for i = 1, #inputOrder do wait()
		local Value = inputOrder[i]
		local Template = script["Template"]:Clone()
		Template.Parent = Gui["Frame"]
		Template["TextLabel"].Text = Value["Text"]
		Template.Name = Value["Text"]
		
		local Tool = Value["Tool"]
		if Tool then
			Template["Item"].Image = Tool.TextureId
			Template.Visible = true
		end
		
		Template["Item"].MouseButton1Down:Connect(function()
			for i, Value in pairs(inputKeys) do
				if Value["Text"] == Template.Name then
					EquipTool(Value["Tool"]) 
				end 
			end
		end)
	end
end

function onKeyPress(inputObject)
	local Key = inputObject.KeyCode.Name
	local Value = inputKeys[Key]
	if Value and UserInputService:GetFocusedTextBox() == nil then
		EquipTool(Value["Tool"])
	end 
end

function EquipTool(Tool)
	if Tool then
		if Tool.Parent ~= Player.Character then
			Player.Character:WaitForChild("Humanoid"):EquipTool(Tool)
		else
			Player.Character:WaitForChild("Humanoid"):UnequipTools()
		end
	end
end

function ChildAdded(Child)
	if Child:IsA("Tool") then
		local isNew = true
		for _, Value in pairs(inputKeys) do
			local Tool = Value["Tool"]
			if Tool then
				if Tool == Child then
					isNew = false
				end
			end
		end
		if isNew == true then
			for i = 1, #inputOrder do wait()
				local Tool = inputOrder[i]["Tool"]
				if not Tool then
					inputOrder[i]["Tool"] = Child
					break
				end
			end
		end
		Configure()
	end
end

function ChildRemoved(Child)
	if Child:IsA("Tool") then
		if Child.Parent ~= Player.Character and Child.Parent ~= Player["Backpack"] then
			for i = 1, #inputOrder do wait()
				if inputOrder[i]["Tool"] == Child then
					inputOrder[i]["Tool"] = nil
					break
				end
			end
		end
		Configure()
	end
end

function Configure()
	for _, Value in pairs (inputKeys) do
		local Tool = Value["Tool"]
		local Item = Gui["Frame"]:FindFirstChild(Value["Text"])
		if Tool then
			Item.Visible = true
			Item["Item"].Image = Tool.TextureId
			if Tool.Parent == Player.Character then
				Item.ImageTransparency = .5
			else
				Item.ImageTransparency = .75
			end
		else
			Item.Visible = false
			Item["Item"].Image = ""
		end
	end
end

return System

1 answer

I'm pretty sure there's a function for adding a value at the end of the table (table.insert),but for adding a value to the first key, I would make another function for that.

local Table1 = {10,20,30,40}
table.foreach(Table1,print)-->
--[[
	1 10
	2 20
	3 30 
	4 40
--]]


local function PlusOneKey(Tab)
	local a = {};
	for b,c in pairs(Tab)do
		a[b+1]=c;
	end;
	return a;
end;

Table1 = PlusOneKey(Table1);
table.foreach(Table1,print)-->
--[[
	2 10
	3 20
	4 30 
	5 40
--]]

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