Buttons not activating when I parent them to a ScollingFrame with UIListLayout in a localscript

I’m trying to add buttons dynamically to a ScrollingFrame with UIListLayout for a character selection screen, however I can’t seem to interact with the buttons when they get added.

This is the localscript that adds the buttons into the scrolling, and it works perfectly fine (example below)

script.Parent.Activated:Connect(function()
	local Data = game.ReplicatedStorage.Remotes.GetPlayerData:InvokeServer(game.Players.LocalPlayer)
	local CharacterSelectFrame = script.Parent.Parent.CharacterSelectFrame
	for i, v in CharacterSelectFrame.ScrollingFrame:GetChildren() do
		if v.Name ~= "UIListLayout" then
			v:Destroy()
		end
	end
	
	for i = 0, Data.CharacterAmount, 1 do
		print(i)
		if i > 1 and Data.PlayerCharacters[i] ~= nil then
			local CharacterButton = game.ReplicatedStorage.ReplicatedAssets.UIElements.CharacterSelectorTemplate:Clone()
			print(Data.PlayerCharacters[i].Name)
			CharacterButton:SetAttribute("Index", i)
			CharacterButton.Text = Data.PlayerCharacters[i].Name
			CharacterButton.Parent = CharacterSelectFrame.ScrollingFrame
		end
	end
	
	
	script.Parent.Parent.CharacterSelectFrame.Visible = not script.Parent.Parent.CharacterSelectFrame.Visible
end)

image
The problem is that the script that is parented to the buttons (or more accurately the script that is a parent to the template) doesn’t seem to be running, and not only that, the buttons don’t even seem to be interactable. Any help or solutions will be greatly appreciated. I’ll leave the script I have parented to the buttons below if it helps, but since it isn’t running in the first place, It seems unlikely for it to be the issue.

script.Parent.Activated:Connect(function()
	print("this is working")
	local Index = script.Parent:GetAttribute("Index")
	local PlayerData = game.ReplicatedStorage.Remotes.GetPlayerData:InvokeServer()
	local CharacterData = PlayerData.PlayerCharacters[Index]
	local Frame = script.Parent.Parent
	
	Frame.Armor.Text = "Armor: "..CharacterData.Equipment.Armor
	Frame.Weapon.Text = "Weapon: "..CharacterData.Equipment.Weapon
	Frame.NameLabel.Text = CharacterData.Name
	Frame.Race.Text = "Race: "..CharacterData.Race.Name
	
	game.ReplicatedStorage.Remotes.CharacterViewEvent:FireServer(Index)
end)
2 Likes

Is the Script enabled? Also are you cloning the scripts into the buttons or making a main script that controls all the buttons?

2 Likes

Cloning the scripts into the buttons. They should be enabled afaik.

1 Like

I do want to clarify, it doesn’t seem like the buttons are even interactable, since they don’t turn slightly darker when I hover them like other interactable UI elements.

1 Like

try replacing below code

script.Parent.Activated:Connect(function()
	print("this is working")

With the code below

script.Parent.MouseButton1Click:Connect(function()
	print("this is working")

I believe you should only use Activated only if the Device that’s playing the game is a Console, i.e. Playstation or XBox.
MouseButton1Click works only for Mobile and Pc, I think :d

Also, make sure the TextButton is actually a TextButton

2 Likes

I should have probably mentioned that I tried that, but yeah it still doesn’t work, Again I’m pretty sure the UI isn’t even interactable since it doesn’t turn grey when I hover like normal interactable UI elements.

1 Like

OH MY GOD I MIGHT BE THE DUMBEST MAN ALIVE. There is a text button, which is what i was trying to clone, but there was also a textlabel by the same name in the same path that I somehow didn’t notice. thanks a bunch man I can’t believe I didn’t notice that.

1 Like

you should really just manage the buttons in 1 script

1 Like

How would I go about doing that with a dynamic amount of buttons?

1 Like

first i’d clean some things up

local RS = game:GetService("ReplicatedStorage")
local GetPlayerData = RS.Remotes.GetPlayerData
local Player = game.Players.LocalPlayer

local CharacterSelectFrame = script.Parent.Parent.CharacterSelectFrame -- Learn to create, and use the variables you made

script.Parent.Activated:Connect(function()
  local Data = GetPlayerData:InvokeServer(Player)
  
  for _, Object in CharacterSelectFrame.ScrollingFrame:GetChildren() do
    if not Object:IsA("UIListLayout") then continue end
    Object:Destroy()
  end

  for i = 1, Data.CharacterAmount do
    if Data.PlayerCharacters[i] == nil then continue end
    local CharacterButton = RS.ReplicatedAssets.UIElements.CharacterSelectorTemplate:Clone()
    
    print(Data.PlayerCharacters[i].Name)
    
    CharacterButton:SetAttribute("Index", i)
    CharacterButton.Text = Data.PlayerCharacters[i].Name
    CharacterButton.Parent = CharacterSelectFrame.ScrollingFrame
  end

  CharacterSelectFrame.Visible = not CharacterSelectFrame.Visible
end)

not sure if i did it the best way, but i prefer this 1 script over a couple dozen scripts

local RS = game:GetService("ReplicatedStorage")
local GetPlayerData = RS.Remotes.GetPlayerData
local CharacterViewEvent = RS.Remotes.CharacterViewEvent
local Player = game.Players.LocalPlayer

local CharacterSelectFrame = script.Parent.Parent.CharacterSelectFrame
local Buttons = {} -- Hold all buttons in an array

local function GetButtons()
  for _, Button: GuiButton? in CharacterSelectFrame.ScrollingFrame:GetChildren() do
    if not Button:IsA("GuiButton") then continue end -- Check if Object is a button
    if table.find(Buttons, Button) then continue end -- Check if button exists

    Button.Activated:Connect(function()
      print("Activated button:", Button.Name)

      local Index = script.Parent:GetAttribute("Index")
      local PlayerData = GetPlayerData:InvokeServer() -- How come you didn't include Player here?
      local CharacterData = PlayerData.PlayerCharacters[Index]
      local Frame = script.Parent.Parent

      Frame.NameLabel.Text = CharacterData.Name
      Frame.Race.Text = "Race: ".. CharacterData.Race.Name
      Frame.Armor.Text = "Armor: ".. CharacterData.Equipment.Armor
      Frame.Weapon.Text = "Weapon: ".. CharacterData.Equipment.Weapon

      CharacterViewEvent:FireServer(Index)
    end)
  end
end

CharacterSelectFrame.ChildAdded:Connect(GetButtons) -- Get all buttons when a new button is added

CharacterSelectFrame.ChildRemoved:Connect(function(Object) -- Remove any buttons that were destroyed
  if not Object:IsA("GuiButton") then return end
  
  local ButtonIndex = table.find(Buttons, Object)
  if not ButtonIndex then return end
  
  Buttons[ButtonIndex] = nil
end)

edit: wrote this all on the forum; don’t expect it to work if you paste it

1 Like

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