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)