Function Key Failed to Initialize

  1. What do you want to achieve?

I want to assign a function to a key in a modular object

  1. What is the issue?

When attempting to initialize a function to the key, it would sometime work and sometime not work. It is very inconsistent

  1. What solutions have you tried so far?

I was thinking it might be a loading issue, but my code has been design for linear execution. I also attempted to wait for a little bit before assigning the function, but that does not work as well.

-- Client Script
for _, buttonFrame in pairs (CollectionService:GetTagged("pushButton")) do
	buttonObjects[buttonFrame.Name] = pushButtonModule.new(buttonFrame)
end

buttonObjects["RollFrame"].onClick = function()
	-- Some block of code here
end

-- Module Script
function pushButton:_Init()
	self.button.MouseButton1Click:Connect(function()
		self.holding = false

		TweenService:Create(self.button, smoothTween,{
			Position = self.hoverPosition
		}):Play()

		if self.onClick ~= nil then
			self.onClick()
		end

		Global_Util.PlaySound(SoundService.click)
	end)

	-- Other additional codes here
end
1 Like

I have done some additional testing, and apparently, the solution I proposed is also very inconsistent. I removed the proposed solution from the post.

The inconsistencies might be happening due to not all pushButtons being loaded at the time CollectionService:GetTagged("pushButton") is called, and if so, the client script will need to be changed like the example below to fix the problem:

-- Client Script
local function addToButtonObjects(buttonFrame)
	buttonObjects[buttonFrame.Name] = pushButtonModule.new(buttonFrame)
end

CollectionService:GetInstanceAddedSignal("pushButton"):Connect(addToButtonObjects)

for _, buttonFrame in CollectionService:GetTagged("pushButton") do
	addToButtonObjects(buttonFrame)
end

buttonObjects["RollFrame"].onClick = function()
	-- Some block of code here
end
1 Like

I have thought of that, which I have made a system so that all UI are loaded on the server rather than having it in StarterGui. I also made the client wait for the server to finish loading before loading everything else in the client.

image

In addition, all buttons have the animation I wanted it to play, I believe the problem remains somewhere in attaching the function/the object might still being created…?

(I tried out your solution just in case it might be the solution, but the function still isn’t initialized to the key :frowning: )

After more thinking I noticed this: You’re looping through each pushButton to add the value returned by the pushButtonModule.new function (which I’m assuming is a table) to the buttonObjects table, using the buttonFrame’s name as the key. If your end goal is to add the onClick function to all tables/pushButtons contained in the buttonObjects table, why not do it like so:

-- Client Script
for _, buttonFrame in CollectionService:GetTagged("pushButton") do
	buttonObjects[buttonFrame.Name] = pushButtonModule.new(buttonFrame)

	buttonObjects[buttonFrame.Name].onClick = function()
		-- Some block of code here
	end
end
1 Like

Each buttonObjects will have their own functions that correlates to its button. I have plans to make it so no buttons will have nil for its onClick. I could probably make table with its correlated functions… but I would like to have a way to assign functions through this current method.

1 Like

If you know the names of the pushButtons in advance, you can store the functions inside of a table and have the key be the pushButton’s name:

-- Client Script
local onClick = {
	ExampleName = function()
		-- Some block of code here
	end,
	ExampleName2 = function()
		-- Other block of code here
	end,
}

for _, buttonFrame in CollectionService:GetTagged("pushButton") do
	buttonObjects[buttonFrame.Name] = pushButtonModule.new(buttonFrame)

	buttonObjects[buttonFrame.Name].onClick = onClick[buttonFrame.Name]
end
1 Like

This is exactly what I was suggesting! Thanks for providing me with your solutions :+1:

1 Like

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