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)
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.
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?
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.
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.
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.
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.