Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

The elseif section doesn't work. Why?

Hi guys! This loop is part of an Elimination round for a Murder Game. I tested on studio, the first "if" works, but when there's only 1 player in the "Playing" team, the "elseif" section should kick in but it doesn't. It seems like I went wrong somewhere. There are no output errors. Please help.

THE CODE IS SOMEWHERE BETWEEN 50 and 60 LINES LONG

while wait()  do
		players = game.Players:GetPlayers()
		if game.Workspace.Time.Value == 0 or game.Players.NumPlayers <2 then
			workspace.Ingame.Value = false
			workspace.Elimination.Value = false
			for i=1,#players do
				if game.Players:FindFirstChild(players[i]) then
				items = players[i].Backpack:GetChildren()
				for b = 1,#items do
					if items[b]:IsA("Tool") then
						items[b]:Destroy()
					end
				end
				tools = players[i].StarterGear:GetChildren()
				for v=1,#tools do
					tools[v]:Destroy()
				end
				players[i].TeamColor = game.Teams["Not Playing"].TeamColor
				wait(0.5)
				players[i]:LoadCharacter()
				end
			end
			game.Workspace:FindFirstChild("Map"):Destroy()
			game.Workspace.Lobby.Noti.SurfaceGui.TextLabel.Round.Text = ""
			game.Workspace.Lobby.Noti.SurfaceGui.TextLabel.Map.Text = ""
			break
		elseif game.Teams.Playing:GetChildren() == 1 then
			winner = game.Teams.Playing[1].Name
			workspace.Ingame.Value = false
			workspace.Elimination.Value = false
			game.Workspace.Time.Value = 0
			for i=1,#players do
				if game.Players:FindFirstChild(players[i]) then
				items = players[i].Backpack:GetChildren()
				for b = 1,#items do
					if items[b]:IsA("Tool") then
						items[b]:Destroy()
					end
				end
				tools = players[i].StarterGear:GetChildren()
				for v=1,#tools do
					tools[v]:Destroy()
				end
				players[i].TeamColor = game.Teams["Not Playing"].TeamColor
				wait(0.5)
				players[i]:LoadCharacter()
				end
			end
			game.Workspace:FindFirstChild("Map"):Destroy()
			game.Workspace.Lobby.Noti.SurfaceGui.TextLabel.Round.Text = ""
			game.Workspace.Lobby.Noti.SurfaceGui.TextLabel.Map.Text = ""
			break
		end
end

2 answers

You wrote game.Teams.Playing:GetChildren() == 1 which is false. GetChildren returns a table, which is not equal to a number. Teams also do not store players, because that's what Players does. On line 28 you wrote game.Teams.Playing[1] which doesn't make sense because it is not a table, it is an object, and you are trying to index a number from an object that has no children.

Here's a function to use instead of pretending that players are inside of teams objects:

function GetPlayersOnTeam(color)
	local t={}
	for _,v in pairs(game.Players:GetPlayers())do
		if v.TeamColor==color then
			t[#t+1]=v
		end
	end
	return t
end
local players = game.Players:GetPlayers()
local playing=GetPlayersOnTeam(game.Teams.Playing.TeamColor)
...

elseif #playing==1 then
	winner=playing[1]
...

1 comment

0

I really like your answer, but someone has already answered on the community chat. Thanks for the help!

The Teams folder does not store the Players currently on that team, so using GetChildren() on it will return an empty Table. Even it is did give you what you expect it to, you have to use the # operator to get the count of Players in the Table, as {...} ~= 1 in any case.

What you need to do is make a function to count the Players on a given team properly:

function countPlayersInTeam(team)
	local tc = team.TeamColor
	local count = 0
	for _, player in ipairs(game.Players:GetPlayers()) do
		if player.TeamColor == tc then
			count = count + 1
		end
	end
	return count
end

and then change line 27 in that code you pasted above:

	        elseif countPlayersInTeam(game.Teams.Playing) == 1 then

This discussion is preserved as it was. New answers, comments, and votes are closed.