Hello. I am new to scripting in roblox studio and in LUA in general. I've gotten pretty far in the game I'm making, but I have one problem. I'm trying to make a script in which everyone is transported to one team when a countdown is over, but I can't get everyone to change the team. This is what I have so far:
local inGame = game.Players:GetPlayers() [#game.Players:GetPlayers()] inGame.TeamColor = BrickColor.new("CGA brown")
The problem is that when I run this script it only has the last person who joined the game join the team, but everyone needs to join at the same time. What I'm looking for is something that works like this:
[1 through #game.Players:GetPlayers()]
Of course, "through" is not an actual function, but I need something that would work like that. Could someone tell me if a function like that exists, and how it works?
Thanks in advanced!
I'll go fast here.
So, you have a table. Let me make an example table.
local tb = { 56456, 4352, 357, 3344 }
Now, we can index this table. Doing tb[1]
would get the first thing in the table, 56456
, doing tb[2]
would return 4352
, and ect.
local first = tb[1] print(first) -- 4352
The hashtag in this instance will count the number of (can't find right word don't correct me this isn't technically write) "things" in the table. For now, this'll do.
print(#tb) -- 4
Now maybe you can understand why you're getting only the last player in the table.
local listOfPlayers = GetPlayers() local lastPlayer = listOfPlayers(#listOfPlayers)
The fix in this case would be to use a loop. Here's an example:
local inGame = game.Players:GetPlayers() for _, plr in next, inGame do plr.TeamColor = BrickColor.new("CGA brown") end
Players = game.Players:GetPlayers() for index = 1,#Players,1 do --From the first player to the last player do Players[index].TeamColor = BrickColor.new("CGA Brown") end
an hashtag symbol associated with a table returns the length of the table,
#{"meme","meme","meme"}
would return 3
You're looking to use math.random to get a random number out of the list of players in the game.
Math.Random
is able to find a random number between two numbers you provide.
Here is the roblox wiki on that.
Be careful because you could also utilize tick() to get a different randomseed every time but it's sorta difficult for a newcomer to get their head around it.
Here it is fixed:
local inGame = game.Players:GetPlayers()[math.random(1,#game.Players:GetPlayers())] --To break it down --The 1 is going to be first player in the server, assuming there is a player. --#game.Players:GetPlayers() is obviously the table but is actually the number of players IN that table.