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

9 comments

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?

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().

0

I not getting any errors, its just nothing will print (or even work) past line 17

0

Using _G is heavily discouraged. Do not use it.

0

well is there anyway I can use other forms of global variables?

0

Can't you use a local variable? Also why you bring politics to roblox?

0
  1. the global variable isnt the problem 2. It's just for me and my friends really!
0

@Oxiousately Did you test the code after you put the two variables inside the function

0

@BestCreativeBoy yes, although it didn't work it was useful to the script