Need help and guidance on module script

Hello guys,
I have a question about module scripts. I’ve created a basic intermission system to my modulescript but the issue is the code gets called once then it won’t run the same line again.

Code for GameManager (serverscript)

-- // SERVICES \\ --
local playerService = game:GetService("Players")
local serverScriptService = game:GetService("ServerScriptService")
-- // MODULE SCRIPT \\ --
local tables = serverScriptService.tables
local startingFunctions = serverScriptService.playerFunctions
-- // TABLE \\ --
local generalPlayers = require(tables).generalPlayers
local playersInField = require(tables).playersInField
local playerFunctions = require(startingFunctions)
-- // GAME CONTROLS \\ --
local minPlayer = 1
local intermissionTime = 10

local function mainRoutine()
	repeat
		task.wait(1)
		warn("Game needs "..minPlayer.. " player(s) to start!")
	until #generalPlayers >= minPlayer
	
	while true do
		for i = intermissionTime, 0, -1 do
			print("Intermission ("..i..")")
			task.wait(1)
			
			if i <= 0 then
				for _, player in pairs(generalPlayers) do
					playerFunctions:AddPlayerToTable(generalPlayers, player) -- Gets called once
				end
			end
		end
		task.wait(1)
		for _, player in pairs(generalPlayers) do
			playerFunctions:RemovePlayerFromTable(generalPlayers, player) -- Gets called once
		end
	end
end

local mainThread = coroutine.create(mainRoutine)

coroutine.resume(mainThread)

playerService.PlayerAdded:Connect(function(player)
	playerFunctions:createLeaderstats(player)
	playerFunctions:AddPlayerToTable(generalPlayers, player)
end)

playerService.PlayerRemoving:Connect(function(player)
	playerFunctions:RemovePlayerFromTable(generalPlayers, player)
	playerFunctions:RemovePlayerFromTable(playersInField, player)
end)

Code for playerFunctions (module script)

local module = {}

function module:createLeaderstats(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local wins = Instance.new("IntValue")
	wins.Name = "wins"
	wins.Parent = leaderstats
	
	local coins = Instance.new("IntValue")
	coins.Name = "coins"
	coins.Parent = leaderstats
	print("Created leaderstats for "..player.Name)
end
function module:AddPlayerToTable(tableName, player)
	if not table.find(tableName, player) then
		table.insert(tableName, player)
	end
	print(player.Name.." has been added")
end
function module:RemovePlayerFromTable(tableName, player)
	for i,v in pairs(tableName) do
		if v.Name == player.Name then
			table.remove(tableName, i)
		end
		print(v.Name.." has been removed")
	end
end
return module

Your removing all the players from generalPlayers in the second part of the loop,
when the loop is run a second time to Add players - the table is empty, so for _, player in pairs(generalPlayers) do doesn’t run.

1 Like

Thank you for your response. I totally forgotten about that. Thank you for reminding me.

2 Likes

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