Hi. I tried making a toolbar but for some reason, it didn't add the tool. In line 63, it's not changing nil to a tool or printing. Also, no errors in the output
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,false) -- Tables and vars local num = { '1', '2', '3', '4', '5', '6', '7', '8', '9', } local numinwords = { 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', } local tools = { One = nil , Two = nil , Three = nil , Four = nil , Five = nil , Six = nil , Seven = nil , Eight = nil , Nine = nil , } local uis = game:GetService('UserInputService') local plr = game.Players.LocalPlayer local backpack = game.Players.LocalPlayer:WaitForChild('Backpack') local frame = script.Parent local char = plr.Character for i, v in pairs(backpack:GetChildren()) do tools[i] = v end --Events backpack.ChildAdded:Connect(function(tool) print('ok') for i, v in pairs(tools) do if v == nil then print('ok2') -- It's not printing 'ok2' v = tool end end for i, v in pairs(tools) do print(tostring(v)) end end) backpack.ChildRemoved:Connect(function(tool) print('ok') for i, v in pairs(tools) do if v == tool then v = nil -- It's not changing it to nil when removed end end for i, v in pairs(tools) do print(tostring(v)) end end) for i, v in pairs(frame:GetChildren()) do if v:IsA('ImageButton') then print('ok') uis.InputBegan:Connect(function(input) if input.KeyCode == Enum.KeyCode[v.Name] then print('ok') if tools[v.Name] ~= nil then char.Humanoid:UnequipTool() char.Humanoid:EquipTool(tools[v.Name]) end end end) end end for i, v in pairs(frame:GetChildren()) do if v:IsA('ImageButton') then v.MouseButton1Click:Connect(function() if tools[v.Name] ~= nil then char.Humanoid:UnequipTool() char.Humanoid:EquipTool(tools[v.Name]) end end) end end
Any help/suggestion is appreciated.
for i, v
creates you 2 local variables, i
and v
, when you are assigning different value to these variables you are changing only variables themselves, if you want to change the key in table, you must index the table with that key:
tools[i] = nil -- It's changing it to nil when removed
Same for the second loop:
-- v = tool tools[i] = tool
It's also true that when you are setting table's key to nil and loop through it, it will ignore the index with nil value so v == nil
is not really possible in your case, I suggest you doing something like:
tools[i] = "None" -- It's changing it to nil when removed
And in check part:
if v == "None" then
You possibly have unexpected behavior here:
local tools = { One = nil , Two = nil , Three = nil , Four = nil , Five = nil , Six = nil , Seven = nil , Eight = nil , Nine = nil , } for i, v in pairs(backpack:GetChildren()) do tools[i] = v end
If you have 3 tools in backpack named "Rocket', "Gun" and "Car" then your table will look like this after the loop:
tools = { One = nil , Two = nil , Three = nil , Four = nil , Five = nil , Six = nil , Seven = nil , Eight = nil , Nine = nil , [1] = Rocket (Instance), [2] = Gun (Instance), [3] = Car (Instance) }
When using print
I don't recommend using tostring
since it automatically converts arguments to string form, however, instead of converting tables to table_x3e48239482 it converts them to { ... } which are tables that you can open in output and see their content, similar to instances I believe instead of printing their name it will print the instance and you can click it which will show it in explorer.