In a game I am working on, I want 2 players to be selected. These 2 players will be chosen from an automatically assigned "spectators" team and placed into opposite teams of "player1" and "player2". Basically a 1v1 match. To sum up, I need help making a script that can randomly choose 2 players from a spectators team, and create a 1v1 match. I also am trying to make sure that: ** a player can't be chosen twice** each player will be teleported to their team corresponding side
I hope I am not asking for too much assistance, but I am clueless. Thanks for reading and I hope you can help!
(This is something I've wrote so far. It's not close to done just showing where I'm going)
local player = game.Players.LocalPlayer local users = game.Players:GetChildren() local function choosePlayers() local users = game.Players:GetChildren() local selectUsers = math.random(1, #users) end while true do wait(3) choosePlayers() end
You are close. You have choosing a random player 50% done. All you have to do now is index the table of players.
What you were doing was generating a number from 1 to the number of players in the game, but not doing anything with it. The table won't index itself on its own.
local function choosePlayers() local users = game.Players:GetPlayers() local user1 = users[math.random(1, #users)] -- index the table returned by GetPlayers local user2 = users[math.random(1, #users)] if user1 == user2 then -- the same player might get selected twice. while user1 == user2 do -- This loop is entered only if the above is true. user2 = users[math.random(1, #users)] end end return user1, user2 -- return the selected users for convenience end while true do wait(3) local user1, user2 = choosePlayers() print(user1, user2) end
This script shouldn't be too much work at all to make, but for a complete beginner it may look complicated. The first thing that we should do is get out of that local script. You can not teleport players from a local script and defining a single player is completely useless. Now that we have that out of the way, we can begin making this happen. we'll start with making a way to tell if a player has been chosen:
game.Players.PlayerAdded:Connect(function(plr) -- run the code inside every time a player joins local Stats = Instance.new("Folder", plr) -- make a folder Stats.Name = "Stats" -- change the folder's name to "Stats" for later reference Stats.Parent = plr -- make the parent of the folder the player local HasPlayed = Instance.new("BoolValue", Stats) HasPlayed.Name = "HasPlayed" HasPlayed.Parent = Stats end)
Now that we have a way to check if a player has been in already, we can start choosing them:
-- I would put this in a different script for organization, but you don't need to local HasNotPlayed = {} -- This table will be used to choose which players can be selected. while wait(3) do -- I'm assuming you wanted to do this once every 3 seconds for some reason? local users = game.Players:GetPlayers() for i, user in pairs(users) do if not user.Stats.HasPlayed then table.insert(HasNotPlayed, user) -- puts every player that hasn't played inside the table end end for i, user in pairs(users) do if user.TeamColor == --[[whatever the team colors for both of the fighters are.]] then user.TeamColor = --[[whatever the spectator team color is]] -- the above code checks if the players were fighting, and if they were, puts them back to the spectator team. end end local Chosen = math.random(1, #users) local ChosenTwo = math.random(1, #users) for i, user in pairs(HasNotPlayed) do if i == Chosen then user.TeamColor = -- Team color of the first fighters team table.remove(HasNotPlayed, i) -- removes the chosen player from the table. -- Teleport the player to the first spawn end if i == ChosenTwo then user.TeamColor = -- Team color of the second fighters team table.remove(HasNotPlayed, i) -- Teleport the player to the second spawn end end local HasPlayed = 0 for i, user in pairs(users) do if user.Stats.HasPlayed then local HasPlayed = HasPlayed + 1 end end if HasPlayed == #users then for i, user in pairs(users) do if not user.Stats.HasPlayed then table.insert(HasNotPlayed, user) end end end end
Tell me if I missed anything in the comments, or if an error occurs.
Hope I helped!
-Cmgtotalyawesome