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

I am having trouble selecting players by team. Any help?

Asked by 5 years ago

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
0
First off, don't define users twice, and use :GetPlayers(). Make a loop that selects a new player using the same way of choosing the first player and check if that random player is the same as the first one. Teleporting is simple, use :MoveTo() or move their HumanoidRootPart. R_alatch 394 — 5y
0
When reading my answer, please hover your mouse over the top write of the code and click "View Source", as the code looks very sloppy with the size constraint of the website. cmgtotalyawesome 1418 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

You were close.

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


Hopefully this answered your question and if it did, then don't forget to hit that "Accept Answer" button. If you have any more questions then feel free to leave them down in the comments below.
0
I tested the script and got this error: "Workspace.playerchoosing:3: bad argument #2 to 'random' (interval is empty)" thebluepicaxe717 18 — 5y
0
Nevermind, I fixed the script and it works!!! thebluepicaxe717 18 — 5y
0
Thankyou soo much for the help! I am very excited to try it. thebluepicaxe717 18 — 5y
0
One more question. How do I connect the user to the character? So I would be able to kill the randomly chosen user if I wanted to. thebluepicaxe717 18 — 5y
0
Either do user.Character.Humanoid.Health = 0 or user.Character:BreakJoints(), but there are many ways to kill a player User#24403 69 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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

0
Too many comments, makes it harder to read. Also why is wait being called in the conditional portion of the loop? Don't do that. OP was following good practices by not doing it. User#24403 69 — 5y
0
Also the Instance.new(type, parent) parent parameter is deprecated just so you know. I would also like to note that on line 37 you are "shadowing" the variable on line 34, not overwriting it. User#24403 69 — 5y
0
@sjr04Alt There is a reason that you can see the source code when you hover your mouse over it, to expand the comment to full length. I apologize that I was trying to help him get a better understanding? cmgtotalyawesome 1418 — 5y
0
Not everyone is aware of that feature... and your answer is so extra lol User#24403 69 — 5y
View all comments (4 more)
0
@sjr04Alt I answered all that he asked besides teleporting, just because you did the minimal amount of work doesn't mean you have to come look at mine and try to make me feel bad about it. Thanks for telling me about the deprecation of the parent parameter but I'd appreciate it if you left me be lmao cmgtotalyawesome 1418 — 5y
0
When did I make you feel bad lol, and you haven't answered my other question, why did you call wait in the conditional portion User#24403 69 — 5y
0
@sjr04Alt I used "feel bad" because I had a lack of better words, sorry. I don't see how using wait in the conditional portion is a bad practice, it works well, saves one line, and doesn't look any less neat. I'm open to having my mind changed about it, but I don' t feel as if I should put in work to change that practice at the moment. cmgtotalyawesome 1418 — 5y

Answer this question