Module Script Value Won't Work In Local Script

Now I’ve heard 1 million different things about how to use module script values in local scripts. But I’ve tired most of them to no avail sadly, I’ll post my script here in hopes that someone knows what’s going on since I’m at a full loss.

-- Module Script
local Teams = {}

Teams.Team1 = 1 --1 = Team1, 2 = Team2, 3 = Team3.
Teams.Team2 = 1 --1 = Team1, 2 = Team2, 3 = Team3.

return Teams
--Local Script
--New LUA Voice Over script for COD Roblox MP game by @Pic3yune.

local Logic = require(game.ReplicatedStorage.new_team_logic)
local Team1 = Logic.Team1
local Team2 = Logic.Team2

local Head = game.Players.LocalPlayer.Character:WaitForChild("Head")

local Lines = nil

if _G.Side == 0 then
    if Team1 == 1 then
		Lines = game.ReplicatedStorage.vox_players.americans:GetChildren()
        if Team1 == 2 then
			Lines = game.ReplicatedStorage.vox_players.british:GetChildren()
            if Team1 == 3 then
                Lines = game.ReplicatedStorage.vox_players.canadian:GetChildren()
                local Voiceline = Lines[math.random(1, #Lines)]:Clone()
                Voiceline.Parent = Head
                print("VOX Success!")
                if _G.Side == 1 then
                    if Team2 == 1 then
                        Lines = game.ReplicatedStorage.vox_players.aipc:GetChildren()
                        if Team2 == 2 then
                            Lines = game.ReplicatedStorage.vox_players.chinese:GetChildren()
                            if Team2 == 3 then
                                Lines = game.ReplicatedStorage.vox_players.africans:GetChildren()
                                local Voiceline = Lines[math.random(1, #Lines)]:Clone()
                                Voiceline.Parent = Head
                                print("VOX Success!")
                            end
                        end
                    end
                end
            end
        end
    end
end
1 Like

Why would you not directly return a table and when you require use :WaitForChild(“ModuleName”) in case the module hasnt been instanced/error.

You could add a getMethod in the module:

Teams.getTeams = function()
return Teams;
end

And in the main you do:

Teams_module.getTeams() there you can handle all data

I’m afraid I don’t know what your talking about, I tried to the best of my ability only to get nothing.

--New team script for COD game by @Pic3yune.

local Teams = {}

Teams.Team1 = 1 --1 = USA, 2 = British, 3 = Canadians.
Teams.Team2 = 1 --1 = AI Player Characters, 2 = Chinese, 3 = Africans.

Teams.getTeams = function()
	return Teams;
end
--New LUA Voice Over script for COD Roblox MP game by @Pic3yune.

local GetLogic = game.ReplicatedStorage:WaitForChild("new_team_logic")
local Team1 = GetLogic.Team1
local Team2 = GetLogic.Team2

local Head = game.Players.LocalPlayer.Character:WaitForChild("Head")

local Lines = nil

if _G.Side == 0 then
    if Team1 == 1 then
		Lines = game.ReplicatedStorage.vox_players.americans:GetChildren()
        if Team1 == 2 then
			Lines = game.ReplicatedStorage.vox_players.british:GetChildren()
            if Team1 == 3 then
                Lines = game.ReplicatedStorage.vox_players.canadian:GetChildren()
                local Voiceline = Lines[math.random(1, #Lines)]:Clone()
                Voiceline.Parent = Head
                print("VOX Success!")
                if _G.Side == 1 then
                    if Team2 == 1 then
                        Lines = game.ReplicatedStorage.vox_players.aipc:GetChildren()
                        if Team2 == 2 then
                            Lines = game.ReplicatedStorage.vox_players.chinese:GetChildren()
                            if Team2 == 3 then
                                Lines = game.ReplicatedStorage.vox_players.africans:GetChildren()
                                local Voiceline = Lines[math.random(1, #Lines)]:Clone()
                                Voiceline.Parent = Head
                                print("VOX Success!")
                            end
                        end
                    end
                end
            end
        end
    end
end

GetLogic.getTeams() 
1 Like

Why are you using _G? It’s completely unreliable!

Also, add some print()s in each if statement, to see which one it gets stopped at.
This isn’t really enough information to help you right now.

It seems to stop working at this line

if Team1 == 1 then
1 Like

Why do you have so much ifs? The problem may be that you need to use elseifs here as the code fkr team2 wont run if the team is not team 1.

1 Like

it can help with debugging sometimes, you can always optimise later!

in the module script, did you put the line:
return Teams?
this is a really common mistake, so it might be the problem.
Im kinda busy rn, so ill follow up later if i find anything else.

This is how the module scripts look currently

--New team script for COD game by @Pic3yune.

local Teams = {}

Teams.Team1 = 1 --1 = USA, 2 = British, 3 = Canadians.
Teams.Team2 = 1 --1 = AI Player Characters, 2 = Chinese, 3 = Africans.

Teams.getTeams = function()
	return Teams;
end
1 Like

Yeah, could you add the return Teams line at the bottom?

I know you have a function to retrieve it, but how will you run the function if you aren’t returning the function?

edit - in your main script you also need to make use of the require statement in the GetLogic variable. It should be: local GetLogic = require(game.ReplicatedStorage:WaitForChild("new_team_logic"))

Done! Sadly it still doesn’t work, how exactly should I call it in the local script?

1 Like

Sorry that I edited into the last message so lazily, but I’ll explain it here.

You can go down two routes, one of which i recommend.
The first solution you could do is replace local GetLogic with local GetLogic = require(game.ReplicatedStorage:WaitForChild("new_team_logic")).getTeams() and remove the last line of the local script (which is GetLogic.getTeams())

The better solution is to replacelocal GetLogic with local GetLogic = require(game.ReplicatedStorage:WaitForChild("new_team_logic")), and just remove all references to the getTeams() function, as it is unnecessary.

edit - just realised this is your original script, maybe the :WaitForChild will do something to help? You should also doublecheck that the module scripts in the right location

This is the script currently

--New LUA Voice Over script for COD Roblox MP game by @Pic3yune.

local GetLogic = require(game.ReplicatedStorage:WaitForChild("new_team_logic"))

local Team1 = GetLogic.Team1
local Team2 = GetLogic.Team2

local Head = game.Players.LocalPlayer.Character:WaitForChild("Head")

local Lines = nil

if _G.Side == 0 then
	if Team1 == 1 then
		Lines = game.ReplicatedStorage.vox_players.americans:GetChildren()
	elseif Team1 == 2 then
		Lines = game.ReplicatedStorage.vox_players.british:GetChildren()
	elseif Team1 == 3 then
		Lines = game.ReplicatedStorage.vox_players.canadian:GetChildren()
		local Voiceline = Lines[math.random(1, #Lines)]:Clone()
		Voiceline.Parent = Head
		print("VOX Success!")
		if _G.Side == 1 then
			if Team2 == 1 then
				Lines = game.ReplicatedStorage.vox_players.aipc:GetChildren()
				if Team2 == 2 then
					Lines = game.ReplicatedStorage.vox_players.chinese:GetChildren()
					if Team2 == 3 then
						Lines = game.ReplicatedStorage.vox_players.africans:GetChildren()
						local Voiceline = Lines[math.random(1, #Lines)]:Clone()
						Voiceline.Parent = Head
						print("VOX Success!")
					end
				end
			end
		end
	end
end
1 Like

this is strange…

(im assuming this isn’t working)

maybe try replacing _G.Side == 0 temporarily just to double check? maybe with just true?
You did say it stopped at if Team1 == 1 then… just want to quickly confirm my suspicions.

with this?

_G.Side == true

nope, just replace the if statement. sorry if i was a little unclear! im in a rush.

Doesn’t work sadly.

if _G.Side == true then
	if Team1 == 1 then
		Lines = game.ReplicatedStorage.vox_players.americans:GetChildren()
	elseif Team1 == 2 then
		Lines = game.ReplicatedStorage.vox_players.british:GetChildren()
	elseif Team1 == 3 then
		Lines = game.ReplicatedStorage.vox_players.canadian:GetChildren()
		local Voiceline = Lines[math.random(1, #Lines)]:Clone()
		Voiceline.Parent = Head
		print("VOX Success!")
		if _G.Side == nil then
			if Team2 == 1 then
				Lines = game.ReplicatedStorage.vox_players.aipc:GetChildren()
				if Team2 == 2 then
					Lines = game.ReplicatedStorage.vox_players.chinese:GetChildren()
					if Team2 == 3 then
						Lines = game.ReplicatedStorage.vox_players.africans:GetChildren()
						local Voiceline = Lines[math.random(1, #Lines)]:Clone()
						Voiceline.Parent = Head
						print("VOX Success!")
					end
				end
			end
		end
	end
end

omg, im so sorry im still very unclear. I’ll just give you the script with the changes made to it.

--New LUA Voice Over script for COD Roblox MP game by @Pic3yune.

local GetLogic = require(game.ReplicatedStorage:WaitForChild("new_team_logic"))

local Team1 = GetLogic.Team1
local Team2 = GetLogic.Team2

local Head = game.Players.LocalPlayer.Character:WaitForChild("Head")

local Lines = nil

if true then --temporary replacement, just for testing my suspicions
	if Team1 == 1 then
		Lines = game.ReplicatedStorage.vox_players.americans:GetChildren()
	elseif Team1 == 2 then
		Lines = game.ReplicatedStorage.vox_players.british:GetChildren()
	elseif Team1 == 3 then
		Lines = game.ReplicatedStorage.vox_players.canadian:GetChildren()
		local Voiceline = Lines[math.random(1, #Lines)]:Clone()
		Voiceline.Parent = Head
		print("VOX Success!")
		if _G.Side == 1 then
			if Team2 == 1 then
				Lines = game.ReplicatedStorage.vox_players.aipc:GetChildren()
				if Team2 == 2 then
					Lines = game.ReplicatedStorage.vox_players.chinese:GetChildren()
					if Team2 == 3 then
						Lines = game.ReplicatedStorage.vox_players.africans:GetChildren()
						local Voiceline = Lines[math.random(1, #Lines)]:Clone()
						Voiceline.Parent = Head
						print("VOX Success!")
					end
				end
			end
		end
	end
end

Hmmm, It seems to have the same results

1 Like

could it be that the teams don’t update properly, or they update AFTER the script runs?

1 Like