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

How would I fix this? A team randomizer script not putting people in their specific teams

Asked by 4 years ago

I am trying to make a script for a round based game where two people get picked to become Presidential nominees and debate, this is the script that chooses the to players and puts them on the two different teams (Democrat and Republican) but it stops working midway through the '_G.Teams' function.

local Teams = game:GetService("Teams")
local blueParty = Teams["Democratic Party"]
local redParty = Teams["Republican Party"]
local playersOnRedTeam = game:GetService("Teams")["Republican Party"]:GetPlayers()
local playersOnBlueTeam = game:GetService("Teams")["Democratic Party"]:GetPlayers()

local function selectPlayer()
    local players = game.Players:GetPlayers()
    local selected = players[math.random(1,#players)]
    return selected
end

_G.Teams = function()
    local randomPlayer = selectPlayer()
    local selectedTeam = math.random(1,2)
    if randomPlayer and randomPlayer.Character then
        if randomPlayer.Team == redParty then
            print("We can't pick this player!")
            wait()
            selectPlayer()
            wait()
            _G.Teams()
        elseif randomPlayer.Team == blueParty then
            print("We can't pick this player!")
            wait()
            selectPlayer()
            wait()
            _G.Teams()
        else
            if selectedTeam == 1 then
                if #playersOnRedTeam >= 1 then
                    randomPlayer.Team = blueParty
                else
                    randomPlayer.Team = redParty
                end
            end
        end
    end
end
0
what exactly is 'midway', are there any errors? Do you know exactly which line? If not, did you try printing tactic, printing few lines and checking whether it prints? imKirda 4491 — 4y
0
Well, keep your variable for playersOnRedTeam and playersOnBlueTeam inside _G.Teams. Because, the variable will be updated once when the game starts, i.e., if you print them, it will print 0. Just put it in your _G.Teams(). BestCreativeBoy 1395 — 4y
0
I not getting any errors, its just nothing will print (or even work) past line 17 Oxiousately 0 — 4y
0
Using _G is heavily discouraged. Do not use it. Dovydas1118 1495 — 4y
View all comments (5 more)
0
well is there anyway I can use other forms of global variables? Oxiousately 0 — 4y
0
Can't you use a local variable? Also why you bring politics to roblox? User#30567 0 — 4y
0
1. the global variable isnt the problem 2. It's just for me and my friends really! Oxiousately 0 — 4y
0
@Oxiousately Did you test the code after you put the two variables inside the function BestCreativeBoy 1395 — 4y
0
@BestCreativeBoy yes, although it didn't work it was useful to the script Oxiousately 0 — 4y

Answer this question