For i,v in pairs not working properly?

I’m getting an issue with my “switch team” buttons loop. Basically, it loops through each button and checks if the player is in the group for them to switch to that team. If not, the button deletes. If so, the button stays and gets a function that switches the players team when pressed using a remote event. I tested with a print statement and confirmed the remote event was not the issue.

The code: (thats giving me the issue)

for i,v in pairs (stuff.Teams:GetChildren()) do
	if v:IsA("TextButton") then
		
		--check if player is in group or not 
		if player:IsInGroup(v.TeamID.Value) then
			if v.TeamID.Value == 8204505 and v.Name ~= "Chaos Insurgency" then
				CheckRank(v, "Class D", 8204505, 1)
				CheckRank(v, "Foundation Personnel", 8204505, 8)			
			else
				print(slot, v.Name)
				v.Position = stuff.Slots:FindFirstChild(slot).Position
				slot = slot + 1
			end
		else
			v:Destroy()
		end
		
		
		--if player clicks on button
		v.MouseButton1Click:Connect(function()			
			if player.Team ~= game.Teams:FindFirstChild(v.Name) and player:IsInGroup(v.TeamID.Value) then
				rep:FireServer(v.Name)
			end
		end)
		
	end
end

If you want to see the full code, I can send it to you, but the only other code is just the CheckRank function and some values, which to my knowledge have no bugs.

1 Like

What is the issue that you are having? Also, try doing ipairs instead of pairs and see if that fixes it.

1 Like

Sorry, forgot to state the issue as I had to retype the post a few times as the forum was having trouble.
When I start the game and press buttons, nothing happens. I tried this with other buttons too, and nothing would print when I told it to. However I am inclined to believe that it is my script that could be causing problems because that is the only script that is dealing with the team selector UI. After sending this message though, I will try copying my code to a new UI and seeing how it works to double check if it is the UI I am using for some reason.

Are you using a Script or a LocalScript? That’s a very important thing you’re not clarifying.

1 Like

Localscript. I just tested a button outside of the screenGUI with a print statement, and it prints, however the buttons to switch teams doesn’t even print when clicked.

A localscript inside a part in workspace won’t work. The localscript will need to be in StarterPlayer or PlayerGui to function.

1 Like

The local script is a child of the frame which all the buttons and UI are a part of.

It could be that you’re executing the loop too early as well. Try adding a wait(5) statement or something which makes sure that the stuff.Teams has all of its children before you iterate it.

1 Like

Make a repo…? Can’t really tell much just from all this.

1 Like

Replying to both of you here:

Sorry, but what is a repo? Repost the question?

Just tried adding a wait before the loop, no change.

A repo is a repository of the project/issue. Just put the gui in a baseplate and upload here.

1 Like

teamselector.rbxm (14.2 KB)
Did I do it properly?

I forgot a major part, the teams, sorry I will send it now:
teamselector.rbxm (14.2 KB)

I’ve been testing and can’t figure out what exactly is causing the problem. However, changing stuff.Teams:GetChildren() to stuff:GetDescendants() managed to fix it on my side.

Edit: It’s because you have buttons on top of each other. Your frame named “slots” has buttons that are on top of your team buttons. Only one of them take the input and it is not the team buttons. After deleting the “slots” frame, your script should work without any changes. stuff.Teams:GetChildren() is the more valid way to do it.

1 Like