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

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

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

✓ Accepted answer

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.

1 comment

0

Thank you, that fixed the problem!

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