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

make math.random limited to a certain team?

Asked by 8 years ago

Okay, so i want to pick 3 random players, but only from a certain team. So instead of using

local players = game.Players:GetPlayers()
math.Random(3 , #players)

what would i use so it is limited to a certain team?

0
You would want to make a function that would return the list of players on that specific team and then be able to use the math.random function on that. M39a9am3R 3210 — 8y

2 answers

Log in to vote
0
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
8 years ago

Problem

In the future, please make an attempt at what you intend to do. Otherwise your post might get locked for not being constructive.


Solution

What the GetPlayers function does not do is give you specific teams. However, there is an easy way around this. We could have a separate function that can return the players on a specific team though. What we want to do is establish the function and use a for loop to go through all players in the Players Service.


Final Script

function GetTeamMembers() --We establish the function GetTeamMembers here.
    local PlayersOnTeam = {} --An empty table currently that we can add onto if someone is on the team.
    for _,v in pairs(game.Players:GetPlayers()) do --For everyone in the game, we want to do something to them.
        if v.TeamColor == BrickColor.new('Bright red') then --Change bright red to the team you want to gather players from.
            table.insert(PlayersOnTeam,v) --v is the player. We're adding the physical player to the table "PlayersOnTeam".=
        end
    end
    return PlayersOnTeam --Once the script has gone through all the players and checked to see if they were on the team, we can return the script to where it was with the table of players.
end

RandomUnluckyGuy = GetTeamMembers()[math.random(#GetTeamMembers())]

If this answer helped, hit that upvote button. If it answered your question, go ahead and hit accept answer. If you have any questions, feel free to leave them in the comments.
0
Should let people try by themselves, they'll just paste your code. Spectrobz 140 — 8y
0
Hey, I did all I could to explain my answer. If they choose not to learn from the post, that's not my problem. If they ask a similar question we can always moderate it as a duplicate question. It's sad that people do go and copy and paste answers and not learn on their own. However, it's the sad reality people cheat, steal, scam, copy, plagiarize, or are flat out too lazy to learn the content. M39a9am3R 3210 — 8y
Ad
Log in to vote
-1
Answered by
Spectrobz 140
8 years ago

You would basically put the players from a specific team in a table. I'll let you figure out how to do it.

Hint http://wiki.roblox.com/index.php?title=Table

Then you would apply the random function to that table.

Answer this question