Function has to be called twice to run

I’m trying to achieve an equip system where you can equip different models/characters into different slots.

The functions that get called when the specific slot is pressed requires it to be double-clicked to have them actually work.

Coroutines caused them to just, not run, period. Not sure what the issue is.

function suitOrCPUButtonsClicked(whichType)
	for _, v in pairs(GUI.Workshop.AnimatronicButtons.Frame.Icons:GetDescendants()) do
		if v:IsA("ImageButton") then
			v.MouseButton1Click:Connect(function()
				if whichType == "Suit" then
					local model = v.Value.Value
					workshopModelRefresh()
					workshopUIRefresh()
					workshopSuitImageRefresh()
					ReplicatedStorage.Events.ClientEvents.ChangeSlot:FireServer(currentSlot, model, "Suit")
				else
					print("CPU STUFF")
					workshopUIRefresh()
				end
			end)
		end
	end
end

“whichType” right now is always Suit.

I’m unsure why this is happening, any help would be much appreciated.

Hello, it is because .MouseButton1Click starts connecting firing after the function is launched once.

What exactly should I do? I should’ve made this apparent (my bad for not clarifying this) but the icons the for loop is pulling don’t exist until another function creates them (to see what you can equip), is that causing any issues?

You can just add suitOrCPUButtonsClicked() to the last line of the script which isn’t inside the function, probably.

Didn’t seem to change anything - the clicking function seems to work just fine, as it still runs prints, it’s just the functions that don’t work until I click twice.

Figured out the issue, it was just lag related issues.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.