Best way to go to next target in a folder after a button is clicked?

Some more context behind what I am doing is, when a player goes to customize their football team (their players) it sends them to the front of the player which is all set up.

What I am trying to achieve is when the button is clicked it will go to the next player in the folder, there are 7 players in this folder, in what way would I be able to simply make the screen go to the next player? A sloppy way I could think of is manually putting every player from 1 to 7 after the button is clicked.

Custom_Remotes.StartEvent.OnClientEvent:Connect(function(TeamName)
	local Tycoon = Workspace:WaitForChild("Tycoon"):WaitForChild("Tycoons"):FindFirstChild(TeamName)
	local Owner = Tycoon.TycoonInfo.Owner
	local Players = Tycoon.PurchasedObjects.Office["Football Team"].Players
	print(TeamName)
	
	--// Start Player Customization Cycle
	Camera.CameraType = Enum.CameraType.Scriptable
	local CenterTween = TweenService:Create(Camera,TweenInfo.new(
		1,
		Enum.EasingStyle.Quad,
		Enum.EasingDirection.In),
		{
			CFrame = Players.C.Animations.SelectionCamera.CFrame -- Player is manually entered here, this is the first player.
		})
		
	CenterTween:Play()
	CenterTween.Completed:Wait() -- The screen is now currently on the player that will not be customized.

	
end)


Should I just manually enter each player when the button is clicked what do you guys think? (this is just a rough draft of what I am trying to do)

You could keep track of the current index, which is then incremented each time the function is called. Use the index to get the nth player from the folder. If the index is more than the number of players in the folder, then just go back to the first index (1).

For example:

local Index = 0
Custom_Remotes.StartEvent.OnClientEvent:Connect(function(TeamName)
	local Tycoon = Workspace:WaitForChild("Tycoon"):WaitForChild("Tycoons"):FindFirstChild(TeamName)
	local Owner = Tycoon.TycoonInfo.Owner
	local Players = Tycoon.PurchasedObjects.Office["Football Team"].Players
	print(TeamName)
	
	Index += 1
	if Index > #Players:GetChildren() then
		Index = 1
	end
	local CurrentPlayer = Players:GetChildren()[Index]
	
	--// Start Player Customization Cycle
	Camera.CameraType = Enum.CameraType.Scriptable
	local CenterTween = TweenService:Create(Camera,TweenInfo.new(
		1,
		Enum.EasingStyle.Quad,
		Enum.EasingDirection.In),
		{
			CFrame = CurrentPlayer.Animations.SelectionCamera.CFrame -- Player is manually entered here, this is the first player.
		})
		
	CenterTween:Play()
	CenterTween.Completed:Wait() -- The screen is now currently on the player that will not be customized.
	
end)

nevermind sorry

i figured it out
thank you for your insight, i will use this its very helpful.

Line 9 automatically determined the number of children. Although looking at it now it has a typo which Ill quickly fix up

1 Like

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