Attempt to call Global 'UpdateColors' (a nil value)

I’m attempting to code a plugin, and I have a function that updates the plugin’s colors, first when you enable the plugin, and then if your theme changes.

Whenever I call “UpdateColors()” it errors. I’m not quite sure what’s going on since I do this exact same thing in my other plugin and it works just fine.

Any help would be much appreciated.

Code:

local MainUi = script.MainUi:Clone()
MainUi.Parent = Plugin

ToolbarButton.Click:Connect(function()
	Plugin.Enabled = not Plugin.Enabled
	if Plugin.Enabled then
		UpdateColors() --It errors here
	end
end)

settings().Studio.ThemeChanged:Connect(function()
	UpdateColors() --And here
end)

function UpdateColors()
	MainUi.BackgroundColor3 = settings().Studio.Theme:GetColor(Enum.StudioStyleGuideColor.MainBackground)

	local Elements = MainUi:GetChildren()
	for _, Element in pairs(Elements) do
		Element:WaitForChild("Amount").TextColor3 = settings().Studio.Theme:GetColor(Enum.StudioStyleGuideColor.ButtonText)
		Element:WaitForChild("Label").TextColor3 = settings().Studio.Theme:GetColor(Enum.StudioStyleGuideColor.ButtonText)
	end
end

You need to define UpdateColors before (above) you call it

2 Likes

I have never heard of defining a function…Could you enlighten me on what that is?

Just put the UpdateColors function and all its contents above the rest of the code that try to call the function.

function UpdateColors()
	MainUi.BackgroundColor3 = settings().Studio.Theme:GetColor(Enum.StudioStyleGuideColor.MainBackground)

	local Elements = MainUi:GetChildren()
	for _, Element in pairs(Elements) do
		Element:WaitForChild("Amount").TextColor3 = settings().Studio.Theme:GetColor(Enum.StudioStyleGuideColor.ButtonText)
		Element:WaitForChild("Label").TextColor3 = settings().Studio.Theme:GetColor(Enum.StudioStyleGuideColor.ButtonText)
	end
end

This part is called defining the function, and it needs to be defined before you call it
I recommend reading PIL, it’s extremely informative!

1 Like
local MainUi = script.MainUi:Clone()
    MainUi.Parent = Plugin

    function UpdateColors()
    	MainUi.BackgroundColor3 = settings().Studio.Theme:GetColor(Enum.StudioStyleGuideColor.MainBackground)

    	local Elements = MainUi:GetChildren()
    	for _, Element in pairs(Elements) do
    		Element:WaitForChild("Amount").TextColor3 = settings().Studio.Theme:GetColor(Enum.StudioStyleGuideColor.ButtonText)
    		Element:WaitForChild("Label").TextColor3 = settings().Studio.Theme:GetColor(Enum.StudioStyleGuideColor.ButtonText)
    	end
    end  
    

    ToolbarButton.Click:Connect(function()
    	Plugin.Enabled = not Plugin.Enabled
    	if Plugin.Enabled then
    		UpdateColors() --It errors here
    	end
    end)

    settings().Studio.ThemeChanged:Connect(function()
    	UpdateColors() --And here
    end)
1 Like

Thank you both! I have it fixed now! :slight_smile:

@AbiZinho

Mark sure to mark a solution. :blush:

1 Like