I can't find a way to constantly check the player's backpack for a new item that has been added such as when the player picks up a new tool. I tried using ChildAdded and ChildRemoved but those events activate every time an item gets added to the backpack which is a problem since when a player equips a tool, it gets added to the character model but when they equip it, it goes back to the backpack which then fires the event.
local player = game.Players.LocalPlayer local char = player.Character local myHum = char:WaitForChild('Humanoid') local backPack = player:WaitForChild('Backpack') local space = script.Space local mainFrame = script.Parent:WaitForChild('MainFrame') local selectedItem = script.Parent.SelectedItem local inventory = {} game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,false) -- disable default backpack Gui --//Search function search(location) for i,v in pairs(location:GetChildren()) do if v:IsA('Tool') then table.insert(inventory,v) end end end --//Add all current items in backpack to inventory search(backPack) search(char) for i,v in pairs(inventory) do local clone = space:Clone() clone.Name = v.Name clone.LayoutOrder = i clone.Image = v.TextureId clone.Parent = mainFrame --//Activated clone.Activated:Connect(function() selectedItem.Value = v if v.Parent == backPack then myHum:EquipTool(v) else myHum:UnequipTools() end end) end --//Create Button function create() backPack.ChildAdded:Connect(function(child) if child:IsA('Tool') then local clone = space:Clone() clone.Name = child.Name clone.Image = child.TextureId clone.Parent = mainFrame end end) char.ChildAdded:Connect(function(child) if child:IsA('Tool') then local clone = space:Clone() clone.Name = child.Name clone.Image = child.TextureId clone.Parent = mainFrame end end) end --//Constantly check for changes while wait() do search(backPack) search(char) create() end
Tables have an array part and a dictionary part. The latter is a collection that is unordered which is a mapping of keys to values.
It generally looks like this
lua
local d = {
[key] = value,
--// you can also do `key = value` if `key` is a valid identifier
};
You can index for value
with d[key]
or d.key
(with the latter assuming key
is a valid identifier).
So what you can do is add the name of the tool that is equipped to this dictionary part.
Something along the lines of this
lua
local equippedTools = {};
And when a child is added to the backpack
lua
if (child:IsA("Tool")) then
if (not equippedTools[child.Name]) then
equippedTools[child.Name] = true;
end
end
Also as a side note don't use while wait() do
. This idiom only works because of a hack; a trick.
Use
lua
while true do
...
wait(t)
end
It's clearer. I also suggest a much longer wait time and you probably can use an event.
Hey Sorukan,
You can accomplish this easily by using logic statements and a
to see if the tool is new. You would just keep all the current Backpack
tools in a table, and when a child is added check if the new child is equal to any of those tools in the table - if it is then it will be not a new tool. Here, let me just show the code rather than explain the whole thing with words:
local players = game:GetService("Players"); -- The Players Service local tools_in_backpack = {}; -- A table that will keep all the tools. It can then check if the new tool exists in this table, and if they don't then that means it's a new tool. local player = players.LocalPlayer; -- The Local Player local backpack = player:WaitForChild("Backpack") -- The backpack function set_table_to_tools() -- Sets the table to all the tools inside the backpack. backpack = player:WaitForChild("Backpack") -- Updates backpack incase the player died. for _, tool in next, backpack:GetChildren() do -- A loop running through all the tools in the backpack to add them. tools_in_backpack[#tools_in_backpack + 1] = tool; -- Adds the instance to the table. end end function fire_upon_newtool(child) -- A function that will fire everytime a new tool is added. if child:IsA("Tool") then -- Checks if what was added is a tool. local new_tool = true; -- A boolean value that will be false if it's not a new tool that was added. for _, tool in next, tools_in_backpack do -- Loops through the table above. if tool == child then -- If any of the tools it is looping through in the table is equal to this new tool, then it's going to set new_tool to false to show that it's not a new tool. new_tool = false; -- Sets new_tool to false because it's not a new tool. end end if new_tool then -- Checks if it's a new_tool print("It's a new tool.") -- Code that will run if it's a new tool. end end end function characteradded_tools(char) -- A function to add all the tools that may be added as the Character is added, because those don't get added since this function is not yet recognized by the server. set_table_to_tools() -- Sets the table to the new tool that are added. end backpack.ChildAdded:Connect(fire_upon_newtool) -- Connects the ChildAdded event to the fire_upon_newtool function. player.CharacterAdded:Connect(characteradded_tools) -- Connects the characteradded_tools function to the CharacterAdded event.
Thanks, Best regards, IdealistDeveloper