Chosen And Chooser Player returned nil even tho randomized properly

i cant see what the error is

but
i check if there is more than 0 players in the game

then i randomize
but the chosen and chooser player returned nil

here is the code


-- Define Some Variables first!

local ReplicatedStorage = game:GetService('ReplicatedStorage')
local ServerStorage = game:GetService("ServerStorage")


local ChosenPlr = nil
local ChooserPlr = nil
local Timer = 20 -- times the game
local CHOSEN = ServerStorage.Chosen
local CHOOSER = ServerStorage.Chooser
local char
local Status2 = ReplicatedStorage.Status2
local plr = game.Players:GetPlayers()
local APlayer = #plr
local Watch = workspace.Watch
local WatchView = workspace.WatchView
local Spawns = WatchView:WaitForChild("Spawns")
local Spawns1 = Watch:WaitForChild("Spawns")

local Status = ReplicatedStorage:WaitForChild("Status")

while true do
	wait(1)
	
	local plrs = {}
	
	while wait() do
		for i, v in pairs(plr) do
			table.insert(plrs, plr)
		end
		
		break
	end
	
	Status.Value = "Waiting For Enough Players!"
	
	repeat wait() until game.Players.NumPlayers >= 1
	
	Status.Value = "Choosing The Chosen And The Chooser Players!"
	
	
	
	
		
	if #plr > 0 then
		ChosenPlr = plr[math.random(1,APlayer)]
		table.remove(plrs, ChosenPlr)
		print(ChosenPlr)
	end
		
	
		
	
	
	
	table.remove(plrs, ChosenPlr) -- remove player the chosen one or the chooser no need for loops because its only 1 player
	
	
	print(ChosenPlr)
	
	
	
	if ChosenPlr ~= nil then
		CHOSEN.Value = ChosenPlr..""
	end
	
	
		
	if #plr > 0 then
		ChooserPlr = plr[math.random(1,APlayer)]
		table.remove(plrs, ChooserPlr)
		print(ChooserPlr)
	end	
			
		
		
		
	
	
	if ChooserPlr ~= nil then
		CHOOSER.Value = ChooserPlr..""
	end
	
	table.remove(plrs, ChooserPlr)
	
	
	
	
	
	--		1				2			3
	--{Chosen.Name} {Chooser.Name} {Watchers}
	
	local SpawnsAvailable = Spawns:GetChildren()
	
	
	local randomspawn = SpawnsAvailable[math.random(1,#SpawnsAvailable)]
	
	local SpawnsAvailable1 = Spawns1:GetChildren()
	
	
	if not SpawnsAvailable and SpawnsAvailable1 then
		print("None Available")
	end
	
	
	for i, player in pairs(plrs, ChooserPlr) do
		if player then
			char = player.Character
			
			if char then
				char:WaitForChild("HumanoidRootPart").CFrame = SpawnsAvailable[1]
			end
		else
			if not player then
				table.remove(plrs, i)
			end
		end
	end
	
	
	
	if ChosenPlr then
		char = ChosenPlr.Character
		
		if char then
			char:WaitForChild("HumanoidRootPart").CFrame = SpawnsAvailable1
		end
	else
		table.remove(plrs,ChosenPlr)
	end
	
	
	
	Status.Value = "Game Is Starting!"
	wait(5)
	Status.Value = "The Chosen Player Is "..ChosenPlr.Name
	wait(5)
	Status.Value = "And The Chooser Is "..ChooserPlr.Name
	
	
	while wait() do
		Timer = Timer - 1
		
		if Timer == 0 then
			break
		else
			print("Timer is still on!")
		end	
		
		break
	end
	
	wait(5)
	
	
	print(ChosenPlr)
	print(ChooserPlr)
	

end

This is because you are creating APlayer and plr before any player joins, change the code in this:

-- Define Some Variables first!

local ReplicatedStorage = game:GetService('ReplicatedStorage')
local ServerStorage = game:GetService("ServerStorage")


local ChosenPlr = nil
local ChooserPlr = nil
local Timer = 20 -- times the game
local CHOSEN = ServerStorage.Chosen
local CHOOSER = ServerStorage.Chooser
local char
local Status2 = ReplicatedStorage.Status2
local plr = game.Players:GetPlayers()
local APlayer = #plr
local Watch = workspace.Watch
local WatchView = workspace.WatchView
local Spawns = WatchView:WaitForChild("Spawns")
local Spawns1 = Watch:WaitForChild("Spawns")

local Status = ReplicatedStorage:WaitForChild("Status")

while true do
	wait(1)
	
	local plrs = {}
	
	while wait() do
		for i, v in pairs(plr) do
			table.insert(plrs, plr)
		end
		
		break
	end
	
	Status.Value = "Waiting For Enough Players!"
	
	repeat wait() until game.Players.NumPlayers >= 1
	
	Status.Value = "Choosing The Chosen And The Chooser Players!"
	
	
	
	
		
	if #game.Players:GetChildren() > 0 then
		ChooserPlr = game.Players:GetChildren()[math.random(1,#game.Players:GetChildren())]
		table.remove(plrs, ChooserPlr)
		print(ChooserPlr)
	end	
		
	
		
	
	
	
	table.remove(plrs, ChosenPlr) -- remove player the chosen one or the chooser no need for loops because its only 1 player
	
	
	print(ChosenPlr)
	
	
	
	if ChosenPlr ~= nil then
		CHOSEN.Value = ChosenPlr..""
	end
	
	
		
	if #game.Players:GetChildren() > 0 then
		ChooserPlr = game.Players:GetChildren()[math.random(1,#game.Players:GetChildren())]
		table.remove(plrs, ChooserPlr)
		print(ChooserPlr)
	end	
			
		
		
		
	
	
	if ChooserPlr ~= nil then
		CHOOSER.Value = ChooserPlr..""
	end
	
	table.remove(plrs, ChooserPlr)
	
	
	
	
	
	--		1				2			3
	--{Chosen.Name} {Chooser.Name} {Watchers}
	
	local SpawnsAvailable = Spawns:GetChildren()
	
	
	local randomspawn = SpawnsAvailable[math.random(1,#SpawnsAvailable)]
	
	local SpawnsAvailable1 = Spawns1:GetChildren()
	
	
	if not SpawnsAvailable and SpawnsAvailable1 then
		print("None Available")
	end
	
	
	for i, player in pairs(plrs, ChooserPlr) do
		if player then
			char = player.Character
			
			if char then
				char:WaitForChild("HumanoidRootPart").CFrame = SpawnsAvailable[1]
			end
		else
			if not player then
				table.remove(plrs, i)
			end
		end
	end
	
	
	
	if ChosenPlr then
		char = ChosenPlr.Character
		
		if char then
			char:WaitForChild("HumanoidRootPart").CFrame = SpawnsAvailable1
		end
	else
		table.remove(plrs,ChosenPlr)
	end
	
	
	
	Status.Value = "Game Is Starting!"
	wait(5)
	Status.Value = "The Chosen Player Is "..ChosenPlr.Name
	wait(5)
	Status.Value = "And The Chooser Is "..ChooserPlr.Name
	
	
	while wait() do
		Timer = Timer - 1
		
		if Timer == 0 then
			break
		else
			print("Timer is still on!")
		end	
		
		break
	end
	
	wait(5)
	
	
	print(ChosenPlr)
	print(ChooserPlr)
	

end
1 Like

Works now im now gone from frustration thanks <3