Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
1

Post all players name on a gui through a loop? [MULTIPLE QUESTIONS]

Asked by 3 years ago

To be honest, I have no idea how I was gonna name this. Still, don't know if that's a good title.

Anyway, I'm making an interview center for a group, and not one of those 1 players GUI application type game, just one of those where you go in, someone interviews you etc. Well, I don't want 10 million people in the same room bothering the two people interviewing. So I'm making a system for that.

So my first question, I made a for i loop, and I did make it right, it posts the textbutton yada yada, but I'm wondering how I post each players name through the loop code:

local players = game.Players:GetChildren()

function SetUpNames()
    gui.Interviewer.SelectPlayer.Size = UDim2.new(1, 0, 0, 500)
    for i = 1, #players do
        local playername = Instance.new("TextButton")
        playername.Name = "PlayerName"
        playername.Size = UDim2.new(0, 200, 0, 25)
        playername.Position = UDim2.new(0, 0, 0, (150*i)-150)
        playername.Text = i --what do i do here?
        playername.Parent = gui.Interviewer.SelectPlayer
    end
end

Second question, How do I detect when the person clicks on a name? I can't do this manually because 1. I don't know everyone's name 2. The amount of people there will be different every time

I do not have any code because I am 100% clear I have no clue how to do it. I don't even know if it's possible, if it isn't, then I'll think of a new way. Probably would have them enter the name with a text box, then with that name search through all the players?

This is all done in a local script, inside the gui.

Any help would be gladly appreciated.

Questions? Comments? Concerns, make sure to comment and I'll try my best to answer them. -John

0
"what do i do here?" i fix it an you downvote me, kid, also you cant detect a players name when they click thehybrid576 288 — 3y
0
'i fix it an you downvote me, kid' what? DeveloperSolo 355 — 3y
0
I never downvoted you DeveloperSolo 355 — 3y

2 answers

Log in to vote
1
Answered by
M39a9am3R 3209 Moderation Voter Community Moderator
3 years ago
Edited 2 months ago

Problem

  1. You're setting the Text property of the TextButton to the index number of the player.

  2. You're not connecting to the TextButton function (Not necessarily a problem, it just works with my Answer format.)

  3. You do not call the SetUpNames function. I am assuming you execute this function but left it out of our view.


Solution

  1. You just need to index the player through the table provided by GetPlayers. Then you would be able to set the property text to the name.

  2. Simple to do, you just connect the variable to the event.

  3. Call the function on the last line of the code.


Recommendation

Get the players while the function is executing. Otherwise it will always return the players who were present when the script was created.


Final Script

function SetUpNames()
    local players = game.Players:GetChildren() --Move this variable into the function. Also, use GetPlayers to ensure the object is a player.   

    gui.Interviewer.SelectPlayer.Size = UDim2.new(1, 0, 0, 500)
    for i = 1, #players do
        local playername = Instance.new("TextButton")
        playername.Name = "PlayerName"
        playername.Size = UDim2.new(0, 200, 0, 25)
        playername.Position = UDim2.new(0, 0, 0, (150*i)-150)
        playername.Text = players[i].Name --What is basically happening is the script will get the element at the "i" position in the table. The brackets are another form of indexing of which is useful in this situation. Then we index that object for a name of the player.
        playername.Parent = gui.Interviewer.SelectPlayer


        playername.MouseButton1Click:Connect(function()
            print(playername.Text)
            print(players[i].Name)
            --Basically, any variables you used in the for loop can be used in the event function. And they will stick to that button.
        end)

    end
end

SetUpNames() --Call the function.

Hopefully this answered your question. If it did, do not forget to hit the accept answer button. If you have any questions, feel free to comment down below.
0
Yea I did hide the call of the function from view, and thanks for the help! DeveloperSolo 355 — 3y
Ad
Log in to vote
-1
Answered by 3 years ago
local players = game.Players:GetChildren()

function SetUpNames()
    gui.Interviewer.SelectPlayer.Size = UDim2.new(1, 0, 0, 500)
    for i,v in pairs(players) --So you can get the index and value of the player
        local playername = Instance.new("TextButton")
        playername.Name = "PlayerName"
        playername.Size = UDim2.new(0, 200, 0, 25)
        playername.Position = UDim2.new(0, 0, 0, (150*i)-150)
        playername.Text = v.Name
        playername.Parent = gui.Interviewer.SelectPlayer
    end
end

Answer this question