Help with functions

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.

image
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()

Template should be in the LocalScript, that way you don’t have to destroy the template and do a lot of work to hide it.

Sure, although this doesn’t help my root issue, which is just getting my event to fire.

You mean CloneUI.MouseButton1Click:Connect(function()?

Activated is only for tools (from what i know), and not gui buttons.

No, the Activated event also works for GUI buttons, I’ve made a Teleport GUI with them.

2 Likes

I swapped it out and it worked. Weird because I’ve used .Activated multiple times before, so I’m not sure what changed.

I highly recommend using MouseButton1Click, .Activated is probably not reliable.

1 Like

I thought MouseButton1Click only worked on computer, so that’s why I went with Activated, although I guess I could be wrong.

I agree, even the developer hub says this event is not browsable in studio, so most likely it is deprecated.

1 Like

Following this, if your game is also playable on mobile, you can consider adding a TouchTap event so it’s compatible for mobile.

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.

Does MouseButton1Click event work on mobile taps?

1 Like

Is there an event that accounts for both mobile and computer?

1 Like

yes, mousebutton1click


1 Like

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