Function firing without having told it to

Okay so long story short, the function is being called without me clicking on the GUI button which is the thing thats supposed to fire it. It prints

{
              [1] = 1,
              [2] = 2,
              [3] = 3,
              [4] = 4
          } Gear  -  Client
1  -  Client
2  -  Client
3  -  Client
4  -  Client
1 1  -  Client
2 2  -  Client
3 3  -  Client
4 4  -  Client
Attempt to connect failed: Passed value is not a function

RIGHT after I press play. After pressing the gui button afterwards just throws the same error, and doesn’t print the numbers. Also, I can’t click on the error. Here’s the full code, pls help

script.Parent.Position = UDim2.new(0.15,0,1,0)
script.Parent.Visible = false

local gear = {1,2,3,4}

local function SwitchCategory(category, stringName)
	print(category, stringName)
	script.Parent.ShopCategoryLabel.MaxVisibleGraphemes = 0
	script.Parent.ShopCategoryLabel.Text = stringName
	for count = 1, string.len(stringName) do
		script.Parent.ShopCategoryLabel.MaxVisibleGraphemes = script.Parent.ShopCategoryLabel.MaxVisibleGraphemes+1
		wait()
		print(script.Parent.ShopCategoryLabel.MaxVisibleGraphemes)
	end
	for i,v in ipairs(category) do
		print(i,v)
	end
end

script.Parent.GearButton.MouseButton1Click:Connect(SwitchCategory(gear, "Gear"))

I think the issue is

You are not providing a function to it, instead you are calling the function and providing it’s result. In order to pass the function you would do:

script.Parent.GearButton.MouseButton1Click:Connect(SwitchCategory)

As that is the actual function. If you wanted to do what you are doing now you would need to do:

script.Parent.GearButton.MouseButton1Click:Connect(function()
	SwitchCategory(gear, "Gear"))
end)
1 Like

Ahh I was thinking that but scrapped the idea. Thanks!

1 Like