Using pairs to select a different child with each click

How do I fix this script so that it only prints 1 name from punch choices? (it prints all the childrens names)
and then how would I make it so when I click it again it doesn’t repeat the same names until all of them have been printed once

foward.MouseButton1Down:Connect(function()
	for _, punchChoice in ipairs(repStorage.PunchChoices:GetChildren()) do
		print(punchChoice.Name)
	end
end)
1 Like
local PunchList = repStorage.PunchChoices:GetChildren()

foward.MouseButton1Down:Connect(function()
	--Random Punch Selection
	local SelectedPunch = PunchList[math.random(1, #PunchList)]
	table.remove(PunchList, table.find(PunchList, SelectedPunch))
	
	--Ordered Punch Selection
	--local SelectedPunch = PunchList[1]
	--table.remove(PunchList, 1)
	
	if #PunchList == 0 then
		PunchList = repStorage.PunchChoices:GetChildren()
	end

	print(SelectedPunch.Name)
end)
2 Likes
local list = {'a', 'b', 'c'}
local index = 0

function iterate()
  index = ((index + 1)%#list)+1
  return list[index]
end

This’ll loop over the list indefinitely, advancing 1 spot whenever iterate is called.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.