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

How do I fix this issue where I try to check the amount of winners and it throws an error?

Hi, I have an issue where I try to check the amount of winners using a table and Teams and it for some reason throws this error, "ServerScriptService.ServerHandler:148: attempt to get length of a nil value"

I have tried to fix this buy making an elseif statement that checks if #Winners is false or nil, and it doesn't work. This is where the issue is:

local Bomb = game.ServerStorage.Bomb
local Coin = game.ServerStorage.Coin
local intermission = 30
local gameTime = 60


while true do

	local bannerTextValue = game:GetService("ReplicatedStorage").Status
	
	local IntermissionMusic = game.Workspace.Music.IntermissionMusic
	local SuccessSound = game.Workspace.Music.SuccessSound
	local LosingHorn = game.Workspace.Music.LosingHorn
	
	IntermissionMusic.Playing = true
	--Intermission

	repeat
        intermission = intermission - 1
		wait(1)
		bannerTextValue.Value = "Intermission: ".. intermission 
	until intermission == 0 
	
	IntermissionMusic.Playing = false
	--Game starts
	if intermission == 0 then
		for _, players in pairs(game.Players:GetChildren()) do
			local MapSpawns = game.Workspace.MapSpawns:GetChildren()
			
			local MapSpawnSelected = MapSpawns[math.random(1, #MapSpawns)]
			
			players.Team = game.Teams["Playing"]
			
			players.Character.Humanoid.Died:Connect(function()
				players.Team = game.Teams["Waiting"]
			end)
			
			players.Character.HumanoidRootPart.CFrame = MapSpawnSelected.CFrame
		end
	
		
		
		repeat
			
			spawn(function()
				local BombCopy = Bomb:Clone()
				BombCopy.Parent = game.Workspace.Bombs
				local xPosition = math.random(-64, 64)
				local zPosition = math.random(-64, 64)
				BombCopy.Position = Vector3.new(xPosition, 100, zPosition)
				wait(4)
			end)
			
			spawn(function()
				local CoinCopy = Coin:Clone()
				CoinCopy.Parent = game.Workspace.Coins
				local xPosition = math.random(-64, 64)
				local zPosition = math.random(-64, 64)
				CoinCopy.Position = Vector3.new(xPosition, 100, zPosition)
				wait(7)
			end)

			wait(1)
			gameTime = gameTime - 1
			bannerTextValue.Value = "Game Time left: " .. gameTime

		until gameTime == 0    
	end

	local function CleanUpGame()
		local terrainToBeRegenerated = game.ServerStorage.MapTerrainBackup.ForestTerrain
		
		local MapBackup = game.ServerStorage.MapBackup

		game.Workspace.Map:Destroy()
		
		for _, coinObject in pairs(game.Workspace.Coins:GetChildren()) do
			coinObject:Destroy()
		end

		for _, Bomb in pairs(game.Workspace.Bombs:GetChildren()) do
			Bomb:Destroy()
		end
		
		
		game.Workspace.Terrain:PasteRegion(
			terrainToBeRegenerated,
			game.Workspace.Terrain.MaxExtents.Min,
			true
		)

		local MapClone = MapBackup:Clone()
		MapClone.Parent = game.Workspace
		MapClone.Name = "Map"
		
		for _, object in pairs(MapClone:GetChildren()) do
			for _, childrenOfObject in pairs(object:GetChildren()) do
				childrenOfObject.Anchored = true
			end
		end
		
		for _, object in pairs(MapBackup:GetChildren()) do
			local TreeTrunk = object.Parent:FindFirstChild("TreeTrunk")
			
			if TreeTrunk then
				local objectWeld = Instance.new("Weld")
				objectWeld.Parent = object
				objectWeld.Part0 = TreeTrunk
				objectWeld.Part1 = game.Workspace.Terrain
				
				for _, object in pairs(MapClone:GetChildren()) do
					for _, childrenOfObject in pairs(object:GetChildren()) do
						childrenOfObject.Anchored = true
						if childrenOfObject.ClassName == "Weld" then
							childrenOfObject.Parent.Anchored = true
						end
					end
				end
			end
		end
	end

	--reset intermission and gameTime to their regular values and clean up map

	intermission = 30
	gameTime = 60

for i, player in pairs(game.Players:GetChildren()) do
		if player.Team == game.Teams["Playing"] then
			local WinnersLobbySpawns = game.Workspace.WinnersLobby.WinnersLobbySpawns:GetChildren() 

			local WinnersLobbySpawnSelected = WinnersLobbySpawns[math.random(1, #WinnersLobbySpawns)]
			
			Winners = {
				
			}


			player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 100
			player.Character.HumanoidRootPart.CFrame = WinnersLobbySpawnSelected.CFrame + 
				Vector3.new(0, 4, 0)

			player.Team = game.Teams["Winners"]
            table.insert(Winners, i, player.Name)
		end
	end
	
	if #Winners then -- error says issue is here
		bannerTextValue.Value = "We have a winner!"
		SuccessSound.Playing = true
		wait(3)
		SuccessSound.Playing = false
		
	elseif #Winners == nil then
		bannerTextValue.Value = "There were no winners :("
		LosingHorn.Playing = true
		wait(5)
		LosingHorn.Playing = false
	end
       
        CleanUpGame()
end

If anyone has an answer, please send an answer!

7 comments

0

The error is on line 148, this script only shows up to line 32.

0

yes, that's because I didn't want to leak all my code for the server handler

0

You are going to have to show the script up to line 148

0

ok i will edit it

0

try printing the winners table, if nothing is printed, it means there is nothing in the table. Here is the code you can use to do so. https://dpaste.org/NgKO#L2

0

Hi Mario, your suggestion didn't work and I still got the same error

0

i dont understand what you're trying to do...

1 answer

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