Problem: When tested with 1 player, it will work fine when you remove the repeat statement that claims there aren't enough players. When tested with 2 players, however, it seems as if it completely skips the for loop on lines 41-53 and throws an error. I found this out by adding a "print(#racers)" after inserting the player into the table, and when done with one player it prints and with 2, it doesn't. I don't understand why this happens at all and I would love to figure this out.

Error: ServerScriptService.MainScript:79: bad argument #1 to 'pairs' (table expected, got nil)

local replicatedstorage = game:GetService("ReplicatedStorage")
local serverstorage = game:GetService("ServerStorage")
local players = game:GetService("Players")

local messagevalue = replicatedstorage:WaitForChild("MessageValue")
local event = replicatedstorage:WaitForChild("RewardGui")

local intermissiontime = 10
local countdowntime = 4
local racetime = 60

local function message(update)
	messagevalue.Value = update
end

local function playeradded(newplayer)
	local leaderstats = Instance.new("IntValue", newplayer)
	leaderstats.Name = "leaderstats"
	
	local points = Instance.new("IntValue", leaderstats)
	points.Name = "Points"
end

players.PlayerAdded:Connect(playeradded)

local function rewardplayer(player, reward)
	if player and reward then
		local leaderstats = player:FindFirstChild("leaderstats")
		
		if leaderstats then
			local points = leaderstats:FindFirstChild("Points")
			
			if points then
				points.Value = points.Value + reward
			end
		end
	end
end

while wait(4) do
	for _, player in pairs(players:GetPlayers()) do
		racers = {}
			
		if player and player.Character then
			local humanoid = player.Character:WaitForChild("Humanoid")
				
			if humanoid and humanoid.Health > 0 then
				table.insert(racers, player)
			end
		end
		
		repeat wait() message("Waiting for players") until #racers >= 2
	end
	
	local intermissiontimer = intermissiontime
	
	for intermission = intermissiontimer, 1, -1 do
		message("Preparing race in " .. intermission)
		wait(1)
	end
	
	message("Loading in a random race track")
	wait(2)
	
	local maps = serverstorage:WaitForChild("Maps"):GetChildren()
	local map = maps[math.random(1, #maps)]:Clone()
	map.Parent = workspace
	wait(2)
	
	message("The chosen race track is " .. map.Name)
	wait(4)

	message("Teleporting all racers to the race track")
	wait(2)
	
	local spawnsmodel = map:WaitForChild("Spawns")
	local spawns = spawnsmodel:GetChildren()

	for _, player in pairs(racers) do
		if player and player.Character and #spawns > 0 then
			local root = player.Character:FindFirstChild("HumanoidRootPart")
			local humanoid = player.Character:FindFirstChild("Humanoid")
			
			local spawnindex = math.random(1, #spawns)
			local randomspawn = spawns[spawnindex]
			
			local activetag = Instance.new("BoolValue", player.Character)
			activetag.Name = "ActiveTag"
			
			if root and humanoid and randomspawn then
				table.remove(spawns, spawnindex)
				root.CFrame = CFrame.new(randomspawn.Position + Vector3.new(0, 2, 0))
				
				humanoid.WalkSpeed = 0
			end
		end
	end
	
	spawnsmodel:Destroy()
	wait(2)
	
	local countdowntimer = countdowntime
	
	for countdown = countdowntimer, 1, -1 do
		message("Race starts in " .. countdown)
		wait(1)
	end
	
	local racetimer = racetime
	
	while racetimer > 0 do
		racetimer = racetimer - 1
		message("Race ends in " .. racetimer)
		
		activeracers = {}
		
		for _, player in pairs(racers) do
			if player and player.Character then
				local humanoid = player.Character:FindFirstChild("Humanoid")
				local activetag = player.Character:FindFirstChild("ActiveTag")
				
				if humanoid and humanoid.Health > 0 and activetag then
					table.insert(activeracers, player)
					humanoid.WalkSpeed = 16
				end
			end
		end
		
		if #activeracers <= 1 then
			break
		end
	end
	
	for _, player in pairs(activeracers) do
		if player then
			if #activeracers <= 1 then
				message("The winner of the race was " .. player.Name .. "!")
				rewardplayer(player, 8)
				event:FireClient(player, "Winner")
			else
				message("Time is up")
				rewardplayer(player, 2)
				event:FireClient(player, "Racer")
			end
		end
	end
	
	map:Destroy()
end

1 comment

0

There must be a error somewhere in the script that makes it not able to go over there because a script runs from top to the bottom then returns to the top so if there is a error it will get stuck and do nothing