Module function inside of a server script refuses to print

Heya Everyone!!

I’m trying to remake a modular round system from a post I made a fairly long time ago to have overall better readability. The issue is that, in the DevForum post, there was a specific lines of code, which is this:

 roundHandler.StartCountdown(function(index) -- Start countdown
        print("Countdown:", index) -- Print the current time
    end)

Whenever I try to copy that, it refuses to print. Why is that?

RoundHandler

--[[MODULE SCRIPT]]--
local RoundModule = require(script:WaitForChild("RoundModule"))

--[[GAMEPLAY LOOP]]--
while task.wait() do
	RoundModule:Intermission(function(INDEX)
		print("Time Remaining:", INDEX)
	end)
	--
	RoundModule:Start()
end

RoundModule

--[[SERVICES]]--
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--[[FOLDERS]]--
local MAPS = ReplicatedStorage:WaitForChild("Maps")

--[[VARIABLES]]--
local Minimum_Players = 3
local Base_Intermission = 3
local Round_Timer = 300

--[[MODULE]]--
local RoundHandlerModule = {}

function RoundHandlerModule:Intermission(INDEX, LIMIT)
	LIMIT = LIMIT or Base_Intermission
	
	for INDEX = LIMIT, 0, -1 do
		task.wait(1)
	end
end

function RoundHandlerModule:Start()
	print("Work")
end

return RoundHandlerModule

Hey there!

It seems you`re trying to pass a function as a first value to the RoundHandler but the ModuleScript refers to the first value, INDEX, as a number, so it causes error.



A potential workaround would be smt like that:

function RoundHandlerModule:Intermission(MANAGER, LIMIT)
	LIMIT = LIMIT or Base_Intermission
	
	for INDEX = LIMIT, 0, -1 do
		MANAGER(INDEX)
		task.wait(1)
	end
end

It works, but it’s only printing the initial countdown and not actually printing out how many seconds left.

(For the module script I gave you, Base_Intermission is 3 seconds. The issue is that it’s only printing 3 seconds and not, y’know, actually printing down. The countdown still works however)

Sorry, my bad. Just change the dot to a colon when you call StartCountdown function. Should work fine!

roundHandler:StartCountdown(function(index) -- Start countdown
	print("Countdown:", index) -- Print the current time
end)

I’ve already done that

--[[MODULE SCRIPT]]--
local RoundModule = require(script:WaitForChild("RoundModule"))

--[[GAMEPLAY LOOP]]--
while task.wait() do
	RoundModule:Intermission(function(INDEX)
		print("Time Remaining:", INDEX)
	end)
	--
	RoundModule:Start()
end

That’s pretty strange. It seems to work fine on my end. Could you double check if you’ve pasted the code correctly or restart your studio?

Nevermind, I solved it myself.

function RoundHandlerModule:Intermission(MANAGER, LIMIT)
	LIMIT = LIMIT or Base_Intermission
	
	--//Intermission Time
	for INDEX = LIMIT, 0, -1 do
		MANAGER(LIMIT)
		LIMIT -= 1
		task.wait(1)
	end
end
1 Like

Hey there! I’m glad @Just0_56 could solve it for you. If you didn’t understand, what happens is that, instead of passing the “manager” function to the RoundHandler:Intermission parameter, you passed “Index” instead, and you can’t because the index is locally implemented in the for loop in :Intermission. What I tried to do previously was to have .StartCountdown handling the intermission, and the current index would be passed as a parameter to manager, so you can do anything while that intermission is running. Good luck! ^^

In this usage case, you are doing this quite wrong, however. You are not supposed to decrease LIMIT, you are supposed to use INDEX itself:

for INDEX = LIMIT, 0, -1 do
		MANAGER(INDEX)

		task.wait(1)
	end

You don’t need to decrease LIMIT each time because INDEX has already this role.

Ah, alright then. Thanks for clarifying it.

1 Like