Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to put player's characters into a table?

Asked by 4 years ago
function Intermission()
    for i, player in pairs(Playing) do
        table.insert(G.Group1, player.Character)
        print(G.Group1[1])
    end
end

Intermission()

(Might've rambled a little so just skip to the question to get straight to the point)

There's so much I'm trying to understand. If I could at least figure out why my for loop I have set up won't run I'll actually start to see some progress for once. I know the function activates; it's not there anymore but I did have something get printed to the output just to see if the function was even starting (it was outside the for loop, anything inside the for loop will not run). Not sure if I'm just ignorant and using the for loop wrong, but any help is appreciated. I've been trying to figure this out for a very long time, I've tried so many things lol.

My Problem

So what I'm trying to do is a lot more complicated than what it looks like, but I'm pretty determined to figure this out without asking for too much help. I'm doing this sort of step by step.

I'm hoping to be able to randomize teams into groups of 5, 3 people per group by the end of all this. Now I haven't really thought about figuring that out yet. I'm just trying to figure out how to put player's characters into a table in general. The code above is supposed to go into Players, iterate through each player and place them into a table called Group1 (That table is actually also in another table called Teams; did that for organization and because there is two other tables/groups but not sure if that's a good idea or if it is a possible issue) .

And to help you understand my code a bit, the variable Playing is local Playing = game.Players:GetChildren()

Question

To sum things up, how could I put the player's characters into a table? Is the way I'm doing it even a vaild way? And why is the for loop getting ignored in the function?

1 answer

Log in to vote
1
Answered by
Nanomatics 1160 Moderation Voter
4 years ago

The way you're doing is a very correct, however your only problem is that you defined Playing as the list of players in the game and you never updated it, so it's just a table of the players that are in the game whenever you defined it, thus, what you need to do is

function Intermission()
    for i, player in pairs(game.Players:GetPlayers()) do
        table.insert(G.Group1, player.Character)
        print(G.Group1[1])
    end
end

Intermission()

That's it, hope I helped, please let me know if you have further questions!

0
Thank you so much. Kind of mad that it was such a simple mistake I just couldn't spot it. I do have one other question. Do you have any suggestions of ways for the script to check if there is players in the game before running? So say a server loads and the code tries to run and everything hasn't loaded yet. Not sure if that should be a worry since I know studio loads things too quick. EstrangedFisherman 51 — 4y
0
it would also be nice for future reference EstrangedFisherman 51 — 4y
0
What I usually do before executing any code that references a variable/player object/any object in general I check if it exists using an if statement, and lets say you want to wait until something exists (example: wait for character to load in) then you type: repeat wait() until player.Character Nanomatics 1160 — 4y
0
That's just to dodge any erroring in the script which causes it to stop running Nanomatics 1160 — 4y
0
Alright cool, thanks Nano. EstrangedFisherman 51 — 4y
Ad

Answer this question