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

Team Randomization script nearly working, but I need some help?

Asked by 3 years ago

Hi, I was making a team randomization script for my game, and none of the ones I tried on the internet worked. So I worked up from the basics of randomizing numbers to where I am now. Here's my code

local random = math.random(1,4)
local player = game.Players:GetPlayers()

if random <= 3 then
    player.Team = game.Teams.TeacherBlu
    player.TeamColor = game.Teams.TeacherBlu.TeamColor
elseif random == 4 then
    print("Didthiswork")

end

So this script is being called on and functioning because when I run the game the output has a 1/4 chance of printing "didthiswork". So the randomization is working. But if I get a value greater than or equal to three then nothing happens. My character doesn't switch teams, it doesn't spit out an error, nothing. So I've deduced that this stems from a problem with the game trying to put the player into a team. The problem is, I don't know what I'm doing wrong. can anyone tell me what's wrong with this part of the script? Thanks in advance.

    player.Team = game.Teams.TeacherBlu
    player.TeamColor = game.Teams.TeacherBlu.TeamColor

1 answer

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

GetPlayers returns a table with every player currently in the game, not any individual player. You can go through the table and set the team of every player in the table with a for loop

for i, player in pairs(game.Players:GetPlayers()) do
    local random = math.random(1, 4) -- get a new random number for every player, otherwise everyone will be on the same team!
    if random <= 3  then
        player.Team = game.Teams.TeacherBlu
        -- setting the TeamColor is not necessary
    else
        print("random number is 4")
    end
end

or, you can use PlayerAdded to set a player's team when they join

game.Players.PlayerAdded:Connect(function(player)
    local random = math.random(1, 4)
    if random <= 3  then
        player.Team = game.Teams.TeacherBlu
    else
        print("random number is 4")
    end
end)
0
@OfficerBrah Sorry but I tried your script and now the script doesn't even run. I'm not seeing anything in the output and my teams have not been randomized. Could I be doing something wrong, just for the record I'm using a regular standard script(not a local or module script) and I'm putting that script in my workplace TheOof1sout 4 — 3y
0
Scripts run as soon as the server is started, before the player has a chance to join, so the code will do nothing because there are no players in the game. That would be how you set random teams at the end of a countdown or something, but you can use the PlayerAdded event to randomize someone's team as soon as they join OfficerBrah 494 — 3y
0
Thanks man, it worked, Idk where I'd be without you. I've been working on this for 3 three days and every path has been a dead end until now. Couldn't thank you enough. TheOof1sout 4 — 3y
Ad

Answer this question