players:GetPlayers() returns nothing

hello yet again my favorite scripters
i have a system that spans over two scripts. one is a module, another is a server script.
i have a module script, here’s how it works.

local module = {}
local player = game:GetService("Players")
local teams = game:GetService("Teams")

function module.AssignCatcher()
	local players = player:GetPlayers()
	if #players > 1 then
	local randomplayer = players[math.random(1, #players)]
	local catcher = players[randomplayer]
	catcher.Team = teams.Catcher
	else
	warn("0 players on the server. wtf?")
	end
end

return module

issue is that, when the players table player:GetPlayers() table is invoked, it always returns nothing.
i tried doing print(game.Players:GetPlayers()) while in testing mode and it returned the current player table, not an empty table.

image

i really have no idea on this one. i tried looking for solutions at other topics, but no dice.

here’s the script that involves the module.

local serverstorage = game:GetService("ServerStorage")
local repstorage = game:GetService("ReplicatedStorage")
local serverscript = game:GetService("ServerScriptService")
local assigncatcher = require(serverscript.AssignCatcher)

local lobby = serverstorage.Lobby:Clone() -- spawn a lobby on init
local map = serverstorage.Map:Clone()

lobby:Clone()
lobby.Parent = game:GetService("Workspace")

repstorage.TimerOver.Event:Connect(function(roundtype)
	if roundtype == "Lobby" then
		lobby:Destroy()
		map:Clone()
		map.Parent = game:GetService("Workspace")
		assigncatcher.AssignCatcher()
	end
end)
1 Like

the code runs before any players actually load
that will be $800

this is a crime

2 Likes

do i just do game.Workspace instead of game:GetService()?

just do workspace
also i didn’t look at the part where you call the module my bad

anyway, just put a task.wait(1) there for debugging purposes and see what happens then

image

oh lmao, there’s a logic error

if there is more than 1 player
do >= and it will work

i just got inflicted with brain damage by past self. thanks.

You are the only one testing so that’s 1, not > 1

I guess to avoid this confusion in the future it may be beneficial to use

if not (#players == 0) then

lol

When would your script run when there aren’t any players in the server in the first place?

I guess it’s not the end of the world to check, but for conditions like I suggest early returns to make these typos less common:

local players = ...
if #players == 0 then
    return
end

-- etc.

This also makes the indentation easier to read.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.