For loop issue on client side

Hello, i am creating a script that when OnClientEvent replaces TextButton texts to current players on server


image
but i have an issue with this script

as you can see i am put here multiple times for some reason, i need that the buttons fill with current players and other leaves with “None” + the client with “You”

1 Like

Can you send the script as a text and not as an image please?

IDK, but that is going to loop through every player for each button, you’ll probably end up with the last player found repeated for each button by doing it that way.

maybe loop through players, then the buttons skipping the button if the text ~= ‘None’

So in your logic:

You loop through the buttons
In each “loop” you loop through the players
If a Player has that name it is named to “You”
You are most likely the only player in your experience at the moment
The loop starts again and checks everytime

I am not sure how you made your logic, but you should change it up

As coolyoshi said, you need to change your logic.
This is just some pseudo-code, and it guesses that there is a button per player.

local playersList = game:GetService("Players"):GetPlayers()
for Index, Buttons in Selects do
   if Buttons:IsA("UIGridLayout") then
      return
   end

   local player = playersList[index]
   if not player then
      return
   end

   if Player.Name == LocalPlayer.Name then
      Buttons.Text = "You"
   else
      Buttons.Text = Player.Name
   end
end

continue should be used instead of return

2 Likes

How should i change it?
if GUI.Enabled == false then
GUI.Enabled = true
for i,Buttons in Selects do
for playerIndex,Player in game:GetService(“Players”):GetPlayers() do
if not Buttons:IsA(“UIGridLayout”) then
if Player.Name == LocalPlayer.Name then
Buttons.Text = “You”
else
Buttons.Text = Player.Name
end
end
end
end
end

image
With this there’s a problem, because of the check if it is UIGridLayout the index is 1 higher

Just add a variable called foundPlayer and increment that as you find players.

How and where, it’s easy to say but hard to understand what you mean

I think he meant that you initialize a variable called playerCount outside the loop, and use that to find players instead of the Index

local playerCount = 1
for _, Buttons in Selects do
   local player = playersList[playerCount]
   if not player then
      return
   end

   playerCount += 1
end
2 Likes