Make Sure Function is There Before Calling in Module?

I have multiple modules, all with the same name, but some have a function called updateModel and others don’t. When I call the updateModel function in a module that doesn’t have the function I get an error. I was wondering if there was a way to check if the function is there before calling it.


Script that errors:

if tile:FindFirstChildOfClass("Model") and tile:FindFirstChildOfClass("Model"):FindFirstChild("Visuals") then
	local mod = require(tile:FindFirstChildOfClass("Model").Visuals)
	
	mod.updateModel() --This is the line that is causing the error.
end

reposted because accidentally posted in bulletin board and replies got locked

1 Like

Modules are tables. You can check if the updateModel value of the module is nil

If module.updateModel then
    module.updateModel()
end

You could also ensure its a function since that would call updateModel if it were anything but nil

If module.updateModel and type(module.updateModel) == 'function' then
    module.updateModel()
end
3 Likes

Perhaps you could try:

if mod.updateModel then
    mod.updateModel()
end

Edit: Thanks to @Fm_Trick for the reminder that there shouldn’t be a () in the condition.