Function data always returns nil

I’m making a game, and I have a utilities script located in ReplicatedStorage. Whenever I try to use the functions located inside that utilities script and put data in them it always returns nil and breaks the game. Why is this happening?

Can you show us the utilities script?

1 Like
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local Lighting = game:GetService("Lighting")
local ServerStorage = game:GetService("ServerStorage")
local Player = Players.LocalPlayer
--[[
	||Checkpoint #1
	--
	||All Utility Functions using "."
--]]

--//Source Code from Devforum//
local Utilities = {}
function Utilities.CreateObject(InstanceData)
	return function(data)
		local Object = Instance.new(InstanceData)
		for a, b in pairs(data) do
			local c, d = pcall(function()
				if type(a) == "number" then b.Parent = Object
				elseif type(b) == "function" then Object[a]:Connect(b)
				else Object[a] = b end end)
			if not c then
				warn("ERROR: Applying property '"..a.."' to "..InstanceData.." failed! ("..d..")")
			end
		end
		return Object
	end
end

function Utilities.PlaySound(
	SoundID, Volume,
	TimePosition, Looped)
	local Sound = Instance.new("Sound")
	for _, SoundData in pairs({SoundID, Volume, TimePosition, Looped}) do
		Sound[SoundData] = SoundData or Sound[SoundData]
	end
	Sound.Parent = workspace:WaitForChild("AudioContainer")
	Sound:Play()
	task.delay(Sound.TimeLength or 200.0,
		function()
			Sound:Remove()
		end)
	return Sound
end

function Utilities.IsMobile()
	local Touch = false
	pcall(function() Touch = UserInputService.TouchEnabled end)
	return Touch
end

--[[
	||Checkpoint #2
	--
	||All Utility Functions using ":"
--]]

return Utilities

The function I’m wanting to work the most is the CreateObject function as it makes my life 100x easier.

What happened here? You need to add a paranthesis to the end of the parameter thing 12412412

Can you show how you are trying to call the CreateObject function? It is actually returning a function so you have to call it with the InstanceData and then use what is returned and call that with the data.

The place with the source code provided this example:

local Logo = CreateObject "ImageLabel" {
		BackgroundTransparency = 1.0,
		Image = ("rbxassetid://"..Images.Logo),
		ImageTransparency = 1.0,
		Position = UDim2.new(0.5, 0, 0.5, 0),
		Size = UDim2.new(0.79, 0, 0.835, 0),
		AnchorPoint = Vector2.new(0.5, 0.5),
		ZIndex = 1.0, Parent = Gui}

And it works using it in the script itself, it’s weird tho because it only breaks when I use it in the module and I don’t want to have this in every single script as that would be ugly and unoptimized.

function Utilities.PlaySound(
	SoundID, Volume,
	TimePosition, Looped)

It closes with parentheses?

Oh my fault, did not see that :sunglasses:

1 Like

So in your local script where you are calling this, are you calling it like this?

local util = require(game:GetService("ReplicatedStorage").ModuleScript) -- Or whatever you named your module script

local Logo = util.CreateObject "ImageLabel" {
	BackgroundTransparency = 0,
	Image = ("rbxassetid://"..Images.Logo),
	ImageTransparency = 1.0,
	Position = UDim2.new(0.5, 0, 0.5, 0),
	Size = UDim2.new(0.79, 0, 0.835, 0),
	AnchorPoint = Vector2.new(0.5, 0.5),
	ZIndex = 1.0, Parent = Gui}

I am calling it in a module script, but yes that is exactly how I am calling it.

Is this located on the Server or the Client?

local Player = Players.LocalPlayer -- Cannot be accessed by the server.

local ServerStorage = game:GetService("ServerStorage") -- Cannot be accessed by the client.

Both the utility and the module script calling the utility are in replicatedstorage.

And where are you calling your module script from that calls the utility function? Actually maybe if you could add a screen shot of how all your files are laid out in the explorer that would help us understand your layout.

1 Like

I see no issues related to the ModuleScript, according to my reproduction tests. The problem may have been located somewhere else.

This is how I structured ReplicatedStorage.
error1
The MainMenu module script is the one calling the function. And it’s being called exactly how I mentioned earlier. And you can see the Utilities module in the replicated modules folder.

Do you have an error in your output related to your issue? Can you try tracing the call stack (the blue messages below the error/warning that shows the order of functions called)? You might possibly find the problematic code somewhere else.

Alright for context. In ReplicatedFirst I have a LocalScript that loads the game, after everything is loaded it initiates the MainMenu module script. Here is the output when the loading script tries to initiate it.


I know why it’s erroring. It’s because for some reason when I try to use the function, it doesn’t detect any data with it, returning nil. I just have no idea why this is happening.

Can you send relevant portions of the MainMenu code, especially line 32?

Here is line 32:

local UIBlip = CreateObject "Sound" {
	SoundId = ("rbxassetid://"..Audio.Blip),
	Name = "MenuBlip",
	Parent = workspace:WaitForChild("AudioContainer")}

And here is the MainMenu module script if it helps:

--//Main Menu Gui//
local MainMenu = {}

--[Services]
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ReplicatedFirst = game:GetService("ReplicatedFirst")
local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")

--[Variables]
local Player = Players.LocalPlayer
local PlayerGui = Player:WaitForChild("PlayerGui")
local AssetsFolder = ReplicatedStorage:WaitForChild("Assets")
local ModulesFolder = ReplicatedStorage:WaitForChild("Modules")

--[Modules]
local Images = require(AssetsFolder:WaitForChild("Images"))
local Audio = require(AssetsFolder:WaitForChild("Audio"))
local Utilites = require(ModulesFolder:WaitForChild("Utilities"))

local CreateObject = Utilites.CreateObject()

--[[
	||Checkpoint #1
	--
	||Functions
--]]
local RightArrow
local LeftArrow
local UIPageLayout

local UIBlip = CreateObject "Sound" {
	SoundId = ("rbxassetid://"..Audio.Blip),
	Name = "MenuBlip",
	Parent = workspace:WaitForChild("AudioContainer")}

function MainMenu:Init()
	--[[
		||Checkpoint #1
		--
		||Creating and Fading Gui In
	--]]
	local Gui = Instance.new("ScreenGui")
	Gui.Parent = PlayerGui
	Gui.Name = "MainMenu"
	Gui.IgnoreGuiInset = true
	
	local FaderBackground = CreateObject "Frame" {
		BackgroundColor3 = Color3.new(0.0, 0.0, 0.0),
		Size = UDim2.new(1.0, 0, 1.0, 0),
		ZIndex = 5.0, Parent = Gui}
	
	local BackgroundImages = {
		Images.MainMenuBackground1,
		Images.MainMenuBackground2}
	local Background = CreateObject "ImageLabel" {
		BackgroundTransparency = 1.0,
		Image = ("rbxassetid://"..BackgroundImages[math.random(1.0,#BackgroundImages)]),
		Size = UDim2.new(1.0, 0, 1.0, 0),
		ZIndex = 1.0, Parent = Gui}
	
	Utilites.PlaySound(Audio.MainMenu, nil, nil, true)
	TweenService:Create(FaderBackground,
		TweenInfo.new(2.0), {BackgroundTransparency = 1.0}):Play()
	task.wait(2.0)
	FaderBackground:Remove()
	
	--[[
		||Checkpoint #2
		--
		||Creating other Objects
	--]]
	local ItemFrame = CreateObject "Frame" {
		BackgroundColor3 = Color3.new(0.0, 0.0, 0.0),
		BackgroundTransparency = 0.5,
		Size = UDim2.new(1.0, 0, 0.275, 0),
		Position = UDim2.new(0.0, 0, 1.0, 0),
		ZIndex = 2.0, Parent = Gui}
	
	local ItemContainer = CreateObject "Frame" {
		BackgroundTransparency = 0.5,
		Size = UDim2.new(0.15, 0, 1.0, -10),
		Position = UDim2.new(0.5, 0, 0.5, 0),
		AnchorPoint = Vector2.new(0.5, 0.5),
		ZIndex = 2.0, Parent = ItemFrame}
	local Container = CreateObject "UIPageLayout" {
		EasingStyle = Enum.EasingStyle.Quint, TweenTime = 0.5,
		HorizontalAlignment = "Center",
		VerticalAlignment = "Center",
		Circular = true, Parent = ItemContainer}
	UIPageLayout = Container
	
	TweenService:Create(ItemFrame,
		TweenInfo.new(1.5, Enum.EasingStyle.Quart, Enum.EasingDirection.Out),
		{Position = UDim2.new(0.0, 0, 0.65, 0)}):Play()
	task.wait(1.0)
	
	local ArrowRight = CreateObject "TextButton" {
		BackgroundTransparency = 1.0,
		Size = UDim2.new(0.0, 30, 1.0, 0),
		Position = UDim2.new(1.0, 0, 0.0, 0),
		Font = Enum.Font.Arcade,
		TextScaled = true,
		TextColor3 = Color3.new(1.0, 1.0, 1.0),
		Text = ">", Name = "RightArrow",
		ZIndex = 3.0, Parent = ItemFrame}
	local RightArrowOutline = CreateObject "UIStroke" {
		Color = Color3.new(0.0, 0.0, 0.0), Thickness = 2.0,
		Parent = ArrowRight}
	TweenService:Create(RightArrow,
		TweenInfo.new(1.0, Enum.EasingStyle.Quart, Enum.EasingDirection.Out),
		{Position = UDim2.new(1.0, -40, 0.0, 0)}):Play()
	
	local ArrowLeft = CreateObject "TextButton" {
		BackgroundTransparency = 1.0,
		Size = UDim2.new(0.0, 30, 1.0, 0),
		Position = UDim2.new(0.0, -30, 0.0, 0),
		Font = Enum.Font.Arcade,
		TextScaled = true,
		TextColor3 = Color3.new(1.0, 1.0, 1.0),
		Text = "<", Name = "LeftArrow",
		ZIndex = 3.0, Parent = ItemFrame}
	local LeftArrowOutline = CreateObject "UIStroke" {
		Color = Color3.new(0.0, 0.0, 0.0), Thickness = 2.0,
		Parent = ArrowLeft}
	TweenService:Create(LeftArrow,
		TweenInfo.new(1.0, Enum.EasingStyle.Quart, Enum.EasingDirection.Out),
		{Position = UDim2.new(0.0, 10, 0.0, 0)}):Play()
	
	RightArrow = ArrowRight
	LeftArrow = ArrowLeft
	task.wait(1.0)
	
	local TopLabel = CreateObject "TextLabel" {
		BackgroundTransparency = 1.0,
		Size = UDim2.new(1.0, 0, 0.0, 40),
		Position = UDim2.new(-1.0, 0, 0.0, 40),
		TextXAlignment = Enum.TextXAlignment.Left,
		Font = Enum.Font.Arcade,
		TextScaled = true,
		TextColor3 = Color3.new(1.0, 1.0, 1.0),
		Text = "Main Menu", Name = "TopLabel",
		ZIndex = 2.0, Parent = Gui}
	local TopLabelOutline = CreateObject "UIStroke" {
		Color = Color3.new(0.0, 0.0, 0.0), Thickness = 2.0,
		Parent = TopLabel}
	TweenService:Create(TopLabel,
		TweenInfo.new(0.5, Enum.EasingStyle.Quart, Enum.EasingDirection.Out),
		{Position = UDim2.new(0.0, 20, 0.0, 40)}):Play()
	
	local VersionLabel = CreateObject "TextLabel" {
		BackgroundTransparency = 1.0,
		Size = UDim2.new(1.0, 0, 0.0, 30),
		Position = UDim2.new(0.0, -20, 1.0, 0),
		TextXAlignment = Enum.TextXAlignment.Right,
		Font = Enum.Font.Arcade,
		TextScaled = true,
		TextColor3 = Color3.new(1.0, 1.0, 1.0),
		Text = ReplicatedFirst:WaitForChild("GameVersion").Value,
		Name = "VersionLabel",
		ZIndex = 2.0, Parent = Gui}
	local VersionLabelOutline = CreateObject "UIStroke" {
		Color = Color3.new(0.0, 0.0, 0.0), Thickness = 2.0,
		Parent = VersionLabel}
	TweenService:Create(VersionLabel,
		TweenInfo.new(0.5, Enum.EasingStyle.Quart, Enum.EasingDirection.Out),
		{Position = UDim2.new(0.0, -20, 1.0, -35)}):Play()
	self.ArrowsReady()
end

function MainMenu.ArrowsReady()
	while true do
		RightArrow.MouseButton1Down:Connect(function()
			UIPageLayout:Next()
			RightArrow.Position = UDim2.new(1.0, -10, 0.0, 0)
			TweenService:Create(RightArrow,
				TweenInfo.new(0.25, Enum.EasingStyle.Back, Enum.EasingDirection.Out),
				{Position = UDim2.new(1.0, -40, 0.0, 0)}):Play() end)
		LeftArrow.MouseButton1Down:Connect(function()
			UIPageLayout:Previous()
			LeftArrow.Position = UDim2.new(0.0, 0, 0.0, 0)
			TweenService:Create(LeftArrow,
				TweenInfo.new(0.25, Enum.EasingStyle.Back, Enum.EasingDirection.Out),
				{Position = UDim2.new(0.0, 10, 0.0, 0)}):Play() end)
	end
end

return MainMenu

You just ran CreateObject and placed the return function with no type to a variable.

local CreateObject = Utilites.CreateObject() -- Returns a function with no type.

Did you mean this?

local CreateObject = Utilites.CreateObject -- Reference the function.