Module Script Functions Not Working

Hey you all. I’m currently making a system that involves choosing a random event to happen (When I say event I mean something that happens in the game, not the actual technical instance.) For some reason, the functions of a main module script I am using for it doesn’t carry onto other scripts. I tried using the actual table and it works, just the functions don’t carry over. I returned something in one of the functions, and when I try to get that returned value with a variable, it says it is nil and errors. I tried looking for reasons why, I couldn’t find anything that could help. Maybe I’m just missing something obvious, although I’m mostly confident that I typed the functions right.

Here is the module script (In ServerStorage)

local module = {}

function initialize()
	
	--Variables
	local categoryFolder = game:GetService("ServerStorage"):WaitForChild("MainEventFolder"):WaitForChild("EventCategories")
	
	--Selects random event
	local category = categoryFolder[math.random(1, #categoryFolder:GetChildren())]
	local event = category:WaitForChild("Events")[math.random(1, #category:WaitForChild("Events"):GetChildren())]
	
	
	--Checks if the event has objects
	if event:FindFirstChild("Objects") then

		for i, object in pairs(event:WaitForChild("Objects"):GetChildren()) do

			--Model
			if object:IsA("Model") then
				
				print("Model in Objects")

			--Part
			elseif object:IsA("Part") then

				print("Part in Objects")

			--Sound	
			elseif object:IsA("Sound") then

				local sound = object:Clone()
				sound.Parent = game:GetService("SoundService"):WaitForChild("CurrentMainEventSounds")

			--Skybox
			elseif object:IsA("Skybox") then
				
				for i, v in game:GetService("Lighting") do
					if v:IsA("Sky") then
						v:Destroy()
					end
				end
				
				local sky = object:Clone()
				sky.Parent = game:GetService("Lighting")
				
				
				if #sky:GetChildren() > 0 then
					
					for i, v in sky:GetChildren() do
						
						v.Parent = game:GetService("Lighting")
						
					end
					
				end

			end


		end

	end
	
	return event

end



function remove(event)

	--Variables
	


	--Checks if the event has objects
	if event:FindFirstChild("Objects") then

		for i, object in pairs(event:WaitForChild("Objects"):GetChildren()) do

			--Model
			if object:IsA("Model") then

				print("Model in Objects")

				--Part
			elseif object:IsA("Part") then

				print("Part in Objects")

				--Sound	
			elseif object:IsA("Sound") then

				game:GetService("SoundService"):WaitForChild("CurrentMainEventSounds"):ClearAllChildren()

				--Skybox
			elseif object:IsA("Skybox") then

				for i, v in game:GetService("Lighting") do
					v:Destroy()
				end

				local sky = game:WaitForChild("ServerStorage"):WaitForChild("Sky"):Clone()
				sky.Parent = game:GetService("Lighting")


				if sky:GetChildren() then
					
					if #sky:GetChildren() > 0 then
						
						for i, v in sky:GetChildren() do

							v.Parent = game:GetService("Lighting")

						end
						
					end
					
				end

			end


		end

	end

end



return module

The functions exist, but they aren’t returned to the requiring module.
Instead of doing

function initialize()

do

function module.initialize()
1 Like

ohhhhhh, I knew I was missing something obvious lol

You have to put the functions inside the module table.
Change your script to this:

local module = {}

function module.initialize()
	
	--Variables
	local categoryFolder = game:GetService("ServerStorage"):WaitForChild("MainEventFolder"):WaitForChild("EventCategories")
	
	--Selects random event
	local category = categoryFolder[math.random(1, #categoryFolder:GetChildren())]
	local event = category:WaitForChild("Events")[math.random(1, #category:WaitForChild("Events"):GetChildren())]
	
	
	--Checks if the event has objects
	if event:FindFirstChild("Objects") then

		for i, object in pairs(event:WaitForChild("Objects"):GetChildren()) do

			--Model
			if object:IsA("Model") then
				
				print("Model in Objects")

			--Part
			elseif object:IsA("Part") then

				print("Part in Objects")

			--Sound	
			elseif object:IsA("Sound") then

				local sound = object:Clone()
				sound.Parent = game:GetService("SoundService"):WaitForChild("CurrentMainEventSounds")

			--Skybox
			elseif object:IsA("Skybox") then
				
				for i, v in game:GetService("Lighting") do
					if v:IsA("Sky") then
						v:Destroy()
					end
				end
				
				local sky = object:Clone()
				sky.Parent = game:GetService("Lighting")
				
				
				if #sky:GetChildren() > 0 then
					
					for i, v in sky:GetChildren() do
						
						v.Parent = game:GetService("Lighting")
						
					end
					
				end

			end


		end

	end
	
	return event

end



function module.remove(event)

	--Variables
	


	--Checks if the event has objects
	if event:FindFirstChild("Objects") then

		for i, object in pairs(event:WaitForChild("Objects"):GetChildren()) do

			--Model
			if object:IsA("Model") then

				print("Model in Objects")

				--Part
			elseif object:IsA("Part") then

				print("Part in Objects")

				--Sound	
			elseif object:IsA("Sound") then

				game:GetService("SoundService"):WaitForChild("CurrentMainEventSounds"):ClearAllChildren()

				--Skybox
			elseif object:IsA("Skybox") then

				for i, v in game:GetService("Lighting") do
					v:Destroy()
				end

				local sky = game:WaitForChild("ServerStorage"):WaitForChild("Sky"):Clone()
				sky.Parent = game:GetService("Lighting")


				if sky:GetChildren() then
					
					if #sky:GetChildren() > 0 then
						
						for i, v in sky:GetChildren() do

							v.Parent = game:GetService("Lighting")

						end
						
					end
					
				end

			end


		end

	end

end



return module
1 Like

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