Attempt to call a Instance value

I have a script where I loop through all the instances in a guiObject and then I activate a variable inside a module script, it goes like this:
LocalScript:

run.MouseButton1Click:Connect(function()
	for _, v in pairs(explorer:GetChildren()) do
		print(v.Name)
		CodeModule[v.Name]()
	end
end)

ModuleScript:

local CodeModule = {
	InsertAPart = Instance.new("Part", workspace)
}

return CodeModule

But when I click the run button it throws an error saying Players.Wildcutepenguin.PlayerGui.BlocksGui.GuiHandler:98: attempt to call a Instance value. Does anyone know the fix for this?

It’s exactly what it says on the tin, you’re treating an instance like a function. It’s hard to solve the problem if we don’t know what you’re trying to achieve.

I thought you could put variables in a modulescript?

Currently the only instance I have under explorer is InsertAPart.

No, you can absolutely have variable in a module script. The problem is that you’re taking the table from the module script, indexing it and getting an instance value and treating the instance like a function.

Edit:
Just to verify, which line is the error coming from?

This bit ⠀⠀

Activating a variable…? What are you trying to achieve?

Also, you are basically looping through explorer's children and checking if one has the name “InsertAPart”. Is that what you’re actually trying to achieve?

After I fix this bug there will be more children and variables but I’m just making sure I start off with no bugs. But for this one, if that instance exists, I want it to insert a part. Other instances in the future will be something like ChangePartColour and ChangePartMaterial etc.

So, here’s how you fix this…

You make “InsertAPart” (your function) a… function…

local CodeModule = {
	InsertAPart = function()
        return Instance.new("Part", workspace)
    end
}

return CodeModule
1 Like

Does it require a function? If so that seems like a waste of space and that will build up over time when I add new things because that uses 3x more lines than the original.

So you want to check if a part in workspace is named “InsertAPart”, you create a new part?

Because that is exactly what your code does, I tested. I’m incredibly confused with what you’re doing here.

Um, yes. Your original code immediately inserted a Part into workspace, did it not?

Modules are evaluated as they are on runtime, meaning just writing Instance.new("Part", workspace) is immediately evaluated, and then InsertAPart is set to said Part.

Functions are the only way to make what you want to happen, happen when you want them.

The code works perfectly now I was just wondering if I needed to write function every time.

1 Like

Pretty sure that’s how programming works.

Oh ok, well at least it works, thanks for the help.

1 Like

Well, yeah but if there was 10 things I wanted to do, then that would take up 30 lines instead of 10.

Let me explain why your old code doesn’t work.

What you were trying to call was a variable, which is ENTIRELY different from a function. A variable stores something, a function runs something.

--VARIABLE
local something = workspace.SpawnLocation

--FUNCTION
local function dosomething()
	warn("no way a function")
end

If you do local part = Instance.new("Part") it stores and runs that though.

Instance.new() is a function that returns whatever you created (which is a part in this case), which you STORE in a variable.

So couldn’t I call that in the module script? If it’s technically a function?

Remember what I said above.

You create the part, store it in the variable. It’s now IN the variable, which means it isn’t a function, it’s a variable. You CANNOT run a variable.


Imagine local part = Instance.new("Part") as local part = workspace.Part.

Both are variables which store parts. You cannot do part() because it is not a function, it’s a variable.