Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

ModuleScript not returning correct value?

Asked by 4 years ago
Edited 4 years ago

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

Log in to vote
0
Answered by 4 years ago

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.

Ad

Answer this question