I have a for loop that creates multiple buttons. I want to have a Button.Activated() registered for each button, although I’m not sure how to pull this offf.
Here’s my code below:
for Index, Map in pairs(Maps) do
if Map and Map.Name then
local CloneUI = Template:Clone()
CloneUI.Parent = GUI
CloneUI.Text = Map.Name
CloneUI.Activated:Connect(function()
warn("Clicked")
ChangeScenery:FireServer(Map.Name)
end)
warn("Registered")
end
end
As you can see I’m trying to create a function in each Button, although it doesn’t register.
Is Maps the buttons? What exactly are Maps? And what exactly is Template (is it the button)? Also, is this given code in a LocalScript or a Server Script? Because the Activated event only fires in a LocalScript. You should probably try using MouseButton1Click.
Yeah sorry for not clarifying! I’m making a GUI that will allow you to change the map/scenery, it’s for something small and meant to be local. “Maps” is a folder in ReplicatedStorage containing the maps that will appear on the GUI and Template is the Template GUI that is being used, I destroy it after the loop Template:Destroy(). And yes, this is in a localscript.
Template is a TextButton that is cloned for my GUI. Here’s my full code (the Scenes script)
And the “Registered” print warns, although not the “Clicked” one when I click the GUIs
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Lighting = game:GetService("Lighting")
local MapsFolder = ReplicatedStorage:WaitForChild("Maps")
local ChangeScenery = ReplicatedStorage:WaitForChild("ChangeScenery")
local GUI = script.Parent
local Template = GUI:WaitForChild("Template")
local Maps = MapsFolder:GetChildren()
for Index, Map in pairs(Maps) do
if Map and Map.Name then
local CloneUI = Template:Clone()
CloneUI.Parent = GUI
CloneUI.Text = Map.Name
CloneUI.Activated:Connect(function()
warn("Clicked")
ChangeScenery:FireServer(Map.Name)
end)
warn("Registered")
end
end
Template:Destroy()
That makes me realize, that Roblox should just make their event names more clear and not a lot of events that do the same thing but only do it based on the device or what’s happening.