Can you pass instances to Modulescript functions?

I’m trying to make a function in a modulescript to keep things organized, but when I try to pass a reference to the part I want it to affect, it seems all the function knows about it is that it exists. Printing the variable used for it results in its name, but printing [variable].name, or any of its values for that matter, returns nil. Can anyone help?

1 Like

Sounds like you’re passing a string, not an instance. Can you show your script?

I’m 99% sure I’m not passing a string, but here’s the script.

Script

local bowl = workspace.Kitchen.MixingBowl
local sign = workspace.Kitchen.ItemSign.ItemsGui
local RepStore = game:GetService("ReplicatedStorage")
local CleanFn = require(RepStore.CleanBowlFctn)

script.Parent.MouseClick:Connect(function(player)
	print("click")
	print(bowl)
	print(bowl:GetFullName())
	--/\ debug stuff /\
	for i, v in pairs(bowl.Ingredients:GetChildren()) do
		local clone = game.ServerStorage[v.name]:Clone()
		clone.Parent = player.Backpack
	end
	CleanFn:Clean(bowl,sign)
end)

Modulescript

local CleanBowl = {}

function CleanBowl.Clean(bowl,sign)
	--start of debug stuff
	print(bowl)
	print(sign)
	print(bowl.name)
	print(bowl:GetFullName()) --this breaks the script and stops the rest of the code from executing :/
	print(bowl.Parent)
	print(bowl:GetChildren())
	print(bowl.Ingredients:GetChildren())
	print(bowl.ItemAmount.Value)
	--end of debug stuff
	bowl.ItemAmount.Value = 0
	bowl.IsThree.Value = false
	for i, v in pairs(bowl.Ingredients:GetChildren()) do
		v:Destroy()
	end
	bowl.EggSplat.Transparency = 1
	sign.Item1.Text = ""
	sign.Item2.Text = ""
	sign.Item3.Text = ""
end

return CleanBowl

Oh, I see the issue. You need to take self as the first argument of your module function, or change CleanBowl.Clean to CleanBowl:Clean, or change your function call to CleanFn.Clean

1 Like

Thank you so much! I read somewhere that using : instead of . passes along the self with the function or something like that, but I didn’t think it was needed for defining functions. It works like a charm, thanks!

1 Like