Why doesn't my function print the player?

Hi, I’m wondering why my module isn’t printing anything, even though it should. Any help would be appreciated!
It’s the ChoosePlayer function that’s supposed to print btw
Here’s the module:

local RoundService = {}

local Players = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage")

function RoundService.ChoosePlayer()
	local players = Players:GetPlayers()
	if #players == 0 then
		return warn("theres no players")
	end

	local chosen = players[math.random(1, #players)]
	print(chosen)
	return chosen
end

function RoundService.TeleportPlayers(cframe)
	local players = Players:GetPlayers()
	for i, player in players do
		if not player.Character then continue end
		
		player.Character:PivotTo(cframe)
	end
end

function RoundService.Countdown(duration)
	for i=duration, 1, -1 do
		workspace:SetAttribute("Clock", i)
		task.wait(1)
	end
end

function RoundService.Intermission(INTERMISSION_TIME)
	workspace:SetAttribute("Status", "Intermission")
	RoundService.Countdown(INTERMISSION_TIME)
end

function RoundService.RunGame(ROUND_TIME,MAP)
	workspace:SetAttribute("Status", "Game")
	RoundService.TeleportPlayers(MAP.CFrame)
	RoundService.Countdown(ROUND_TIME)
	RoundService.ChoosePlayer()
end
return RoundService

Server script:

local Players = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage")

local RoundService = require(RS.Modules.Services.RoundService)
local MIN_PLAYERS = 1

while true do
	task.wait()
	if #Players:GetPlayers() >= MIN_PLAYERS then
		RoundService.Intermission(10)
		
		workspace:SetAttribute("Status", "Loading map...")
		local V_MAPS = RS.Assets.Maps.Vanilla:GetChildren()
		local RANDOM_MAP = V_MAPS[math.random(1,#V_MAPS)]
		local NEW_MAP = RANDOM_MAP:Clone()
		NEW_MAP.Parent = workspace
		task.wait(1)
		
		RoundService.RunGame(60,NEW_MAP.Spawn)
	else
		workspace:SetAttribute("Status", "Waiting for players...")
	end
end

Everything BUT the ChoosePlayer works, please help.

1 Like

You’re not choosing a player until the round ends?

RoundService.Countdown(ROUND_TIME) -- Aka. 60 seconds
RoundService.ChoosePlayer()
1 Like

Yeah lol, stupid me… Thanks a lot!!!

Happens.

On another note, For your yielding functions, I recommend you suffix them with “async”. This clarifies that code proceeding its call cannot run until the function has ended

where? could you implement that to the script? :)))

Yielding functions are functions that pause the caller thread. Roblox’s documentation outlines these functions. The most notable yielding function is task.wait. Any function which calls a yielding function becomes a yielding function, so you would make these changes:

CountdownCountdownAsync
IntermissionIntermissionAsync
RunGameRunGameAsync

So i just add Async to the end of every functions name?

If a function is classified as a yielding function, you end its name with “Async”

ohhh like the stuff with task.wait in them

1 Like

what does it do tho to add async at the end of the name

“This clarifies that code proceeding its call cannot run until the function has ended”

In other words, it ensures you, and anyone else, are aware the function being called will yield the thread

1 Like

can you explain a little simpler though?

It’s more just a formatting thing.
Roblox functions end in Async if that function can yield. In simpler terms, it will essentially do task.wait() or some other type of waiting (yielding).

For example, the function BadgeService:UserHasBadgeAsync will not always immediately return you if the user has a certain badge, but rather it’ll yield for a bit while it queries the answer from Roblox’s database.

This is not mandatory, nor will it actually change anything about how the code works, it just clarifies what the function does a bit more.

2 Likes

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