Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to find and name all tools in backpack?

Asked by 4 years ago

Okay,

Basically I have a script that checks through your backpack to read out all of the tools the player has in output.

If it is working, when the player steps on a brick, it should go in the output and read out the tools something like this:

Tool A, Tool B, Tool C, -- continues going

Currently, my code looks like this and I can't seem to figure out how to convert the table into a string and get it all in one print. I have been successful at getting it out in 3 seperate prints using unpack() but that's not what I wanted to do.

local c = plr.Backpack:GetChildren()
local backpack = {}

for i = 1,#c do
    if c[i]:IsA("Tool") then
        table.insert(backpack, c[i])
    end
end

print() -- output goes here

Thanks, tgarbrecht

0
Did you print(backpack)? If that does not work, maybe try this: print(backpack:GetChildren())? Soban06 410 — 4y
0
yes, printing backpack by itself will just return a table value and backpack:GetChildren() errors. i've been on this for hours and can't seem to find out a way to get it formatted correctly or even work without formatting it tgarbrechtALTacc 20 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

It seems like you were attempting to print out the tool instances, but wanted to print out the tool names. This should work:

local c = plr.Backpack:GetChildren()
local backpack = {}

for i = 1,#c do
    if c[i]:IsA("Tool") then
        if i == #c then
            table.insert(backpack, c[i].Name)
        else
            table.insert(backpack, c[i].Name..", ")
        end
    end
end

print(unpack(backpack))  --This will print each of the values in "backpack", each separated by a comma (except for the last value)
0
Thank you so much, it works. Any way to separate it with commas or no? tgarbrechtALTacc 20 — 4y
0
Nevermind, figured it out on my own. tgarbrechtALTacc 20 — 4y
0
Yep. If anything, I updated the post so that the values are separated by commas. Alexrocks911 15 — 4y
Ad

Answer this question