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

Why aren't my players being added to the table?

My players are not being added to the table "battling". It runs the loop and as soon as it exits it prints all are dead therefore ending the game.

while wait(1) do
		matchTime = matchTime - 1
		print("Run")
		local battling = {}
		while looptime > 0 do
			wait()
			looptime = looptime - 1
			for _, player in pairs (contestants) do
				if player then
					local character = player.Character
					if character then
						local humanoid = character:FindFirstChild("Humanoid")
						local matchTag = character:FindFirstChild("MatchTag")
						if matchTag and humanoid and humanoid.Health > 0 then
							table.insert(battling, player)
						end
					end
				end
			end
		end
		if #battling < 1 then
			print("All are dead")
			break
		elseif #battling == 1 then
			print("We have a winner")
		elseif #battling > 1 then
			print(matchTime)
		elseif matchTime <= 1 then
			matchTime = timeConst
			break
		end
	end

Any help will be appreciated.

Here is the full script if needed:

wait(5) -- wait for players character to load

local intermission = 10
local timeConst = 120 -- this is the time of the match. It makes sure that the time is always correct
local matchTime = timeConst -- 2 minutes
local firstRun = true
local message = Instance.new("Message")

local ss = game:WaitForChild("ServerStorage")
local gear = ss:WaitForChild("Gear")
local maps = ss:WaitForChild("Maps")

local plrs = game:WaitForChild("Players")
local teams = game.Teams

while wait(1) do
	if firstRun == true then
		print("First Run") -- debug
		wait(1)
		firstRun = false
	end
	
	while true do
		print("First Loop Initiated")
		contestants = {}
		wait(1) -- allow time for all contestants to load
		for k,plr in pairs (game.Players:GetChildren()) do
			if plr and plr.Character then
				print("player and char")
				local humanoid = plr.Character:FindFirstChild("Humanoid")
				if humanoid and humanoid.Health > 0 then
					print("Humanoid")
					table.insert(contestants, plr)
				end
			end
		end
		if #contestants >= 2 then
			print("contestants")
			wait(1)
			break
		else
			print('waiting for players')
		end
		wait(1)
	end
	
	----------------[LoadMap]--------------------
	--local randomMap = maps[math.random(1, #maps)]:Clone()
	--randomMap.Parent = game.workspace
	-- The above is commented to be fixed later.
	-- For now I will use the map loading system that only allows one map
	ss.Maps.ForestMap.Parent = workspace
	print("Map should be loaded")
	---------------------------------------------
	
	--------------------[Intermission]------------------------------
	while wait(1) do
		print("Intermission Loop Initiated")
		if intermission ~= 0 then
			intermission = intermission - 1
			message.Parent = workspace
			message.Text = "Game begins in: "..intermission..""
		elseif intermission <= 0 then
			message.Parent = nil
			wait(0.5)
			break
		end
	end
	---------------------------------------------------------------
	
	local numofplayers = game.Players.NumPlayers -- loop once for every player
	local looptime = numofplayers -- This may be a bit buggy. May need to update it after the end of match.
	
	while wait(1) do
		if looptime > 0 then
			looptime = looptime - 1
			
		end
		for _, player in pairs (contestants) do
			print("Preparing character for match")
			if player and player.Character then
				print("Player and player's char loaded")
				-- I removed the torso variable as I couldn't see it's point
				local humanoid = player.Character:FindFirstChild("Humanoid")
				if humanoid then
					print("Humanoid detection")
					humanoid.Health = 100 -- Not sure what the point of this is
				end
				matchTag = Instance.new("StringValue")
				matchTag.Name = "MatchTag"
				player.TeamColor = teams.Battling.TeamColor
				deathScript = ss:WaitForChild("DeathScript"):Clone()
				deathScript.Disabled = false	
			end
		end
		if looptime <= 0 then
			break
		end
	end
	
	for i, p in pairs (contestants) do
		p:LoadCharacter()
		matchTag.Parent = p.Character -- If the player dies the matchtag will be removed automatically. Saves a little work for me.
		deathScript.Parent = p.Character
		local allGear = gear:GetChildren()
		local randomGear = allGear[math.random(1, #allGear)]:Clone()
		randomGear.Parent = p.Backpack
	end
-- Problem begins here V--
	while wait(1) do
		matchTime = matchTime - 1
		print("Run")
		local battling = {}
		for _, player in pairs (contestants) do
			if player then
				local character = player.Character
				if character then
					local humanoid = character:FindFirstChild("Humanoid")
					local matchTag = character:FindFirstChild("MatchTag")
					if matchTag and humanoid and humanoid.Health > 0 then
						table.insert(battling, player)
					end
				end
			end
		end
		if #battling < 1 then
			print("All are dead")
			break
		elseif #battling == 1 then
			print("We have a winner")
			for i,v in pairs (battling) do
				if v.Character:FindFirstChild("MatchTag") then
					print("Winner is "..v.."")
				end
			end
		elseif #battling > 1 then
			print(matchTime)
		elseif matchTime <= 1 then
			matchTime = timeConst
			break
		end
	end
	
	wait(5)
	for _, player in pairs (contestants) do
		if player and player.Character then
			local humanoid = player.Character:FindFirstChild("Humanoid")
			local matchTag = player.Character:FindFirstChild("MatchTag")
			player.TeamColor = game.Teams.Lobby.TeamColor
			if humanoid then
				humanoid:UnequipTools()
			end
			if matchTag then
				matchTag:Destroy()
			end
		end
		local Backpack = player:FindFirstChild("Backpack")
		if Backpack then
			Backpack:ClearAllChildren()
			print("Backpack cleared")
		end
		if player.Character:FindFirstChild("DeathScript") then
			player.Character.DeathScript.Parent = nil
		end
	end
	game.workspace:WaitForChild("ForestMap").Parent = ss
end

3 comments

0

This script was not originally by me. Someone asked me to re-write the script. I decided to start from scratch reading his script and re-writing it. I fixed the problems he was having but created some of my own. This is the only one that I can't seem to fix on my own.

0

I added a comment that shows where my problem begins so you no longer have to look for it.

0

I took a break from this for a while and decided to work on it some more. After some time I got it to add a player to the table. I also learned that the for loop that adds the matchtag into the player is only run once. This is what is causing the problem.

1 answer

It's probably not in that script. Are you sure character.MatchTag exists at the time of checking?

6 comments

0

Yes. The matchtag does exist. This script runs the whole game aside from any weapons.

0

The part above is the part that is breaking.

0

Well then are you sure Contestants contains anything?

0

Yes. If it wasn't the script would never have made it to this loop.

0

I'm going to post the whole script on this page.

0

Wow okay my jiob just suddenly became not my job

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