Why am I getting the error: 17:21:22.279 - ServerScriptService.Script:14: attempt to compare number and nil

I am trying to make a deathrun game, but whenever I playtest it I get the error 17:21:22.279 - ServerScriptService.Script:14: attempt to compare number and nil
script:

local RunnerSpawn = game.Workspace.RunnerSpawn
local TrapperSpawn = game.Workspace.TrapperSpawn
local Roles = {"Runner","Trapper"}
local MaxRunner = 5
local MaxTrapper = 1
local CurrentRunners = 0
local CurrentTrapper = 0
local Role = ""
function pickRoles()
	for i,player in pairs(game.Players:GetChildren()) do --//As i said before, v was player here, so i changed it.
		for i,v in pairs(game.Players:GetChildren()) do
			for i,v in pairs (Roles, #Roles) do
				Role = Roles[math.random(#Roles)]
					if Role == "Runner" and CurrentRunners < MaxRunners then
						print('Max runners')
						Role = "Trapper"
						player.Character.Torso.Cframe = TrapperSpawn.CFrame
                        CurrentTrapper = 1
					else
					 if Role == "Trapper" and CurrentTrapper < MaxTrapper then
						print('Max Trappers')
						Role = "Runner"
						player.Character.Torso.Cframe = RunnerSpawn.CFrame
                        CurrentRunners = CurrentRunners + 1
					end
				end
			end
		end
	end
end
wait(10)
pickRoles()

Simple mistake, you defined MaxRunner as 5, but line 14 tries to look for MaxRunners, which is undefined. Just remove that S

Happens to the best of us. Good luck!

1 Like

error: 17:29:12.130 - Torso is not a valid member of Model
how would I define torso? Also, it prints ‘Max Runners’ but there is only one runner and the max runners is 5

You might want to use either Upper or Lower Torso since I am pretty sure Torso is only R6.

1 Like

When I do that I get error: 17:34:56.381 - Cframe is not a valid member of MeshPart

CFrame has a capital F.
30characters

1 Like

Well put brackets around this.

Also

If you’re using r15 rigs, the torso is split into Upper and Lower.

It’s CFrame, not Cframe

1 Like

I’ve done

local RunnerSpawn = game.Workspace.RunnerSpawn
local TrapperSpawn = game.Workspace.TrapperSpawn
local Roles = {"Runner","Trapper"}
local MaxRunner = 5
local MaxTrapper = 1
local CurrentRunners = 0
local CurrentTrapper = 0
local Role = ""
function pickRoles()
	for i,player in pairs(game.Players:GetChildren()) do --//As i said before, v was player here, so i changed it.
		for i,v in pairs(game.Players:GetChildren()) do
			for i,v in pairs (Roles, #Roles) do
				Role = Roles[math.random(#Roles)]
					if Role == "Runner" and {CurrentRunners < MaxRunner} then
						print('Max runners')
						Role = "Trapper"
						player.Character.UpperTorso.CFrame = TrapperSpawn.CFrame
                        CurrentTrapper = 1
					else
					 if Role == "Trapper" and {CurrentTrapper < MaxTrapper} then
						print('Max Trappers')
						Role = "Runner"
						player.Character.UpperTorso.CFrame = RunnerSpawn.CFrame
                        CurrentRunners = CurrentRunners + 1
					end
				end
			end
		end
	end
end
wait(10)
pickRoles()

it teleports players, but it says max trappers even though no one was the trapper

This is a table, by putting it after the and operator, all that happens is that the script checks if it exists at all. it doesnt check the return of the operation inside of it. Use regular brackets.

That same thing happens here

1 Like

like this?

local RunnerSpawn = game.Workspace.RunnerSpawn
local TrapperSpawn = game.Workspace.TrapperSpawn
local Roles = {"Runner","Trapper"}
local MaxRunner = 5
local MaxTrapper = 1
local CurrentRunners = 0
local CurrentTrapper = 0
local Role = ""
function pickRoles()
	for i,player in pairs(game.Players:GetChildren()) do --//As i said before, v was player here, so i changed it.
		for i,v in pairs(game.Players:GetChildren()) do
			for i,v in pairs (Roles, #Roles) do
				Role = Roles[math.random(#Roles)]
					if Role == "Runner" and [CurrentRunners < MaxRunner] then
						print('Max runners')
						Role = "Trapper"
						player.Character.UpperTorso.CFrame = TrapperSpawn.CFrame
                        CurrentTrapper = 1
					else
					 if Role == "Trapper" and [CurrentTrapper < MaxTrapper] then
						print('Max Trappers')
						Role = "Runner"
						player.Character.UpperTorso.CFrame = RunnerSpawn.CFrame
                        CurrentRunners = CurrentRunners + 1
					end
				end
			end
		end
	end
end
wait(10)
pickRoles()

The devforum is not a place to go to and get all your errors fixed by someone else. There are exceptions to this, as some errors genuinely cannot be solved by yourself and that is perfectly fine, however I gave you a guide on how roblox errors work yesterday, I hoped it would help you get rid of errors without needing to copy and paste the error onto the devforum.
In this case, you’re doing something like like nil > Number, comparing a number to a nil (undefined, non existent) value. If you aren’t sure why something is erroring, doing a google search should be your first go to, how googling Cframe is not a valid member of MeshPart lead to this wiki article. Hopefully this helps with your future coding.

2 Likes

Use these (ctrl 9, ctrl 0)

30char

1 Like

Your variable is incorrectly written compared to the previously declared ones. Try to make sure you type your variables exactly as it says. Hence case sensitivity and spelling.

For instance, you declared MaxRunner but you compared MaxRunners.

3 Likes

For your problem with Max runner print even though there is no runner. Your mistake is rather simple. In the if statement you used this condition

[CurrentRunner < MaxRunner]

The same goes with the trapper one

[CurrentTrapper < MaxTrapper]

And you know that in the line above it. You set the CurrentRunner to 0. In which, make CurrentRunner is lesser than MaxRunner. And so the condition [CurrentRunner < MaxRunner] is true. Thus, running the code below. The simple solution is to change the condition to something like this
[CurrentRunner == MaxRunner]

1 Like