Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

ModuleScript not returning correct value?

I'm making a game where there is a red team and a blue team, and each team has their own timer. There is a bank button where your coins that you collected on the map go into a separate leaderboard. All of that works, but I want the team's timer to go to 0 when everyone on the team has banked their coins. I am trying to do this by comparing the number of people on that team that clicked the button to the number of people on that team.

The modulescript that holds the members of the teams prints the people on the team correctly when the game starts, but when you click on the button, it lists an empty table.

blue team module:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local newGame = ReplicatedStorage.Timers.NewGame


module = {}

Players.PlayerRemoving:Connect(function(player)
	for i, v in pairs(module) do
		if v == player then
			table.remove(module, i)
		end
	end
end)

-- fired at the very end of intermission, refreshes teams
newGame.Event:Connect(function()
	module = {}
	for i, v in pairs(Players:GetPlayers()) do
		if v.Team == Teams.Blue then
			print(v.Name)
			table.insert(module, v)
		end
	end
	print(module)
end)


return module

red team module:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local newGame = ReplicatedStorage.Timers.NewGame

module = {}

Players.PlayerRemoving:Connect(function(player)
	for i, v in pairs(module) do
		if v == player then
			table.remove(module, i)
		end
	end
end)

-- fired at the very end of intermission, refreshes teams
newGame.Event:Connect(function()
	module = {}
	for i, v in pairs(Players:GetPlayers()) do
		if v.Team == Teams.Red then
			print(v.Name)
			table.insert(module, v)
		end
	end
	print(module)
end)

return module

button:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerScriptService = game:GetService("ServerScriptService")
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local bank = script.Parent
local bankRequest = ReplicatedStorage.ItemEvents.BankRequested
local bankEvent = ReplicatedStorage.ItemEvents.BankTouched
local blueTeam = require(ServerScriptService.BlueTeamModule)
local redTeam = require(ServerScriptService.RedTeamModule)
local blueTimer = ReplicatedStorage.Timers.BlueTimer
local redTimer = ReplicatedStorage.Timers.RedTimer
local banked 
local newGame = ReplicatedStorage.Timers.NewGame

local blueBanked = {}
local redBanked = {}

bank.ClickDetector.MouseClick:Connect(function(player)
	
	banked = bankRequest:InvokeClient(player) -- asks client for banked value
	if banked == false then
		bankEvent:FireClient(player) -- sets banked to true
		player.leaderstats.Banked.Value = player.leaderstats.Coins.Value
		player.leaderstats.Coins.Value = 0
		if player.Team == Teams.Blue then
			table.insert(blueBanked, player)
			print(blueBanked)
			print(blueTeam) -- empty table, should list all the blue team players
			if #blueBanked == #blueTeam then
				blueTimer.Value = 0
				blueBanked = {}
			end
		elseif player.Team == Teams.Red then
			table.insert(redBanked, player)
			print(redBanked)
			print(redTeam) -- empty table, should list all the red team players
			if #redBanked == #redTeam then
				redTimer.Value = 0
				redBanked = {}
			end
		end
	end
end)

1 answer

Ok, so I now realize that modulescripts are the wrong tool for what I'm trying to do (just store a value that all other scripts can access) and that _G is a MUCH better solution. I'm storing the tables with the team members in global variables rather than a module.

This discussion is preserved as it was. New answers, comments, and votes are closed.