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

How do I make that every button has a name of a tool?

Asked by
JoneXI 51
4 years ago

Hello, I have a problem. There are 4 text buttons and 3 tools. I want that those text button's names would be the names of the tools. It prints "It worked" 12 times and all of the button names are a one tool name.

Script:

function refresh()  
        print("refresh play")
for i,v in pairs(script.Parent.Frame:GetChildren()) do
        if v:isA("TextButton") then
            table.insert(buttons, v)
            print("button added into table")
        end
end

for i,v in pairs(buttons) do
    for u,o in pairs(player.items:GetChildren()) do
        buttons[i].Text = o.Name
        print("it worked")
        end
    end
end 
wait(10)
refresh()

Thank you.

1 answer

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

The issue lies in the loop

for i,v in pairs(buttons) do -- button loop
    for u,o in pairs(player.items:GetChildren()) do -- item loop
        buttons[i].Text = o.Name
        print("it worked")
        end
    end
end 

For every button you then get all items and set the item name to the same button meaning it will be set to the last item name in the loop.

You want to store the items then index them per button

local itms = player.items:GetChildren()

for i,button in pairs(buttons) do
    if itms[i] then
        button.Text = itms[i].Name -- index the table of items
    else
        -- no tool for button
    end
end

Look at using events over refresh wait loops. You only need to refresh if something changed.

I hope this helps. Please comment if you have any other question about this code.

0
Thank you, that fixed the problem! JoneXI 51 — 4y
Ad

Answer this question