local collectServ = game:GetService("CollectionService")
for i, part in pairs(collectServ:GetTagged("customizationIconButton")) do
part.MouseButton1Down:Connect(function()
print('e')
game.ReplicatedStorage.changeShirt:FireServer(part.Parent, part.Name)
end)
end
I do this to all items tagged. This doesn’t seem to do anything, however, when I clone them into my GUI, and clicking them does nothing.
I’m cloning them into the playergui, so I’m thinking that because they are cloned, and the for loop has already ran, it doesn’t apply to the new clone? How can I fix this? What is the most efficient way to determine when to run the for loop again?
Other possible problem?: The assets haven’t loaded in when the collection service script runs, meaning the script hasn’t looked over all these tagged items
Don’t loop through all the tagged things again. Just run some code whenever a new tagged thing is created. You can do that with CollectionService:GetInstanceAddedSignal(tag).
E.g.
local TagS = game:GetService("CollectionService")
local ChangeShirtEvent = game.ReplicatedStorage.changeShirt
local CUSTOMIZATION_BUTTON_TAG = "customizationIconButton"
--Make customization buttons signal the server to change the player's shirt
function setupCustomizationButton(button: GuiButton)
button.MouseButton1Down:Connect(function()
print(CUSTOMIZATION_BUTTON_TAG, button:GetFullName())
ChangeShirtEvent:FireServer(button.Parent, button.Name)
end)
end
--Handle buttons that exist before this script runs
for _, tagged in ipairs(TagS:GetTagged(CUSTOMIZATION_BUTTON_TAG)) do
setupCustomizationButton(tagged)
end
--Handle buttons that are added after this script runs
TagS:GetInstanceAddedSignal(CUSTOMIZATION_BUTTON_TAG):Connect(setupCustomizationButton)
the “button: GuiButton” is something ive never seen before as a way to set up a parameter… explain?
1 Like
It’s a Luau type hint, telling the language that whatever is passed as the first parameter to that function should have a specific type. In this case, GuiButton is a built in type. TextButton and ImageButton both inherit MouseButton1Down from GuiButton. Using type hints tends to improve intellisense / autocomplete suggestions and can even underline with red in case you try calling the function with something that’s not a GuiButton.