Why am I getting the Error 12:46:47.623 - Workspace.Main:101: attempt to index nil with 'Character'

I am trying to make all players die, and get coins when a round is over, but I keep getting the error 12:46:47.623 - Workspace.Main:101: attempt to index nil with ‘Character’
script

--SpyxSpy main script
game.Players.PlayerAdded:Wait()

local remoteEvent = game.ReplicatedStorage:FindFirstChild("GUIEvent")

local timeEvent = game.ReplicatedStorage:FindFirstChild("timeEvent")

local RoundGuiText = game.StarterGui.RoundGui.Frame.TextLabel

local MinPlayers = script.MinPlayers

local MinPlayersValue = script.MinPlayers.Value

local spawns = {"Spawn1", "Spawn2", "Spawn3", "Spawn4", "Spawn5", "Spawn6"}

local selectedSpawn = spawns[math.random(1, #spawns)]

local Teleport = selectedSpawn

local spawn = workspace[selectedSpawn]

local IntermissionTime = 20

local timerTag = game.ReplicatedStorage:FindFirstChild("timerTag")

local RoundTime = 240

local players = game.Players:GetPlayers()

local ClassGui = game.StarterGui.ClassGui

local ClassGuiFrame = ClassGui.Frame

ClassGuiFrame.TextLabel.Visible = false

local roles = { --our roles table, where the name means the maximum members
    {Name = "Scientist", Max = 6},
    {Name = "Spy", Max = 1},
    {Name = "Soldier", Max = 2}
}
local playerRoles = {} --will store what players have what roles

function getRoleMembers(roleName) --returns a table of players that have a role
    local playerTable = {}
    for player,role in pairs(playerRoles) do
        if role == roleName then
            playerTable[#playerTable + 1] = player
        end
    end
    return playerTable
end

function getRolesFull() --checks if all the roles are full
    for _,roleTbl in pairs(roles) do
        if #getRoleMembers(roleTbl.Name) < roleTbl.Max then
            return false
        end
    end
    return true
end

function getRole() --get a random role name that isnt full
    if getRolesFull() then
        return nil
    end
    local chosen
    repeat
        chosen = roles[math.random(1,#roles)]
    until #getRoleMembers(chosen.Name) < chosen.Max
    return chosen.Name
end

for _,player in pairs(players) do --select each player's role
    local selectedRole = getRole()
	playerRoles[player] = selectedRole
end

target = spawn
local function Start()
  for i = 15, 0, -1 do 
    IntermissionTime = i 
    game:GetService("ReplicatedStorage").IntermissionTime.Value = i 
    wait(1)
  end 
 -- The code below will teleport the players to the target as soon as the count finishes
local selectedRole = getRole()
  for i, player in ipairs(game.Players:GetChildren()) do
  remoteEvent:FireClient(player,selectedRole)
  if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
   target = workspace["Spawn"..math.random(1,6)]
   player.Character.HumanoidRootPart.CFrame = target.CFrame
  end 
end
  -- After all players are teleported, the round will start counting down 
  for i = 120, 0, -1 do 
    RoundTime = i
    game:getService("ReplicatedStorage").RoundTime.Value = i  
    wait(1)
  end
	for i,v in pairs(players) do
		player.Character.Torso:Remove()
		player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 25
	end
end 

while true do 
  wait()
  Start()
  wait()
end

Start()

leaderstat script

game.Players.PlayerAdded:Connect(function(player)

local f = Instance.new("Folder", player)
f.Name = "leaderstats"

local value1 = Instance.new("IntValue", f)
value1.Name = "Coins"
value1.Value = 0
end)

I’m fairly sure you need to use v.Character and v.leaderstats instead of player, since you only use player in the first for loop. Either that, or just replace v with player like you have done in the rest of your loops.

Quick guide on how errors work:
the format is like this:
time the error happened - the script:the line of the script: the error description

in your case, the error was on line 101, and this is the code there:

player.Character.Torso:Remove()

why is it erroring? From the error we can see that its trying to “index nil with Character.” So it looks like player is a nil value, but why? If we open script analysis from the view tab, we can see this: image
lines 101 and 102 don’t have the variable player, because in your loop, you do i,v not i,player.

A few extra things: try and use :Destroy() and not :Remove(), as :Remove() is Deprecated. Wiki link
Instead of using spawn try and name a variable something like CurrentSpawn or Spawn with a capital s, as spawn is already a function.

1 Like