Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I check for a mouse button click on every child of a scrolling frame?

Asked by
NewGPU 36
4 years ago
Edited 4 years ago

Hello, friends! I have the following script

local frame = script.Parent

Instance.new("UIListLayout", frame) -- Automatically spaces the buttons for you

for _,v in pairs(game.ReplicatedStorage.mythings:GetChildren()) do
    local button = Instance.new("TextButton", frame)
    button.Text = v.Name
    button.BackgroundColor3 = Color3.new(232, 67, 147)
    button.BorderColor3 = Color3.new(170, 255, 255)
    button.TextColor3 = Color3.new(255, 255, 255)
    button.TextSize = 14
    button.Size = UDim2.new(0, 146,0, 19)
    button.Font = "GothamBlack"
    button.TextScaled = true

    local buttonBG = Instance.new("TextButton", button)
    buttonBG.BackgroundColor3 = Color3.new(109, 110, 108)
    buttonBG.BorderColor3 = Color3.new(170, 255, 255)
    buttonBG.size = UDim2.new(1, 0,0.05, 0)
    buttonBG.Position = Vector2.new(0, 0, 1.02, 0)


end

frame.CanvasSize = UDim2.new(1,0,0,#game.ReplicatedStorage.mythings:GetChildren()*25)

And what it does is get all the children of a folder in ReplicatedStorage and makes buttons in a scrolling frame for them

so what I'd like to do is change a value in the player's backpack to the name of the button they pressed but I wouldn't want to have to put scripts in every single one of the buttons is there any way I could check for button presses in all of them with one script?

If so, please try and give me details on how to do so.

fyi: this is not a request

1 answer

Log in to vote
1
Answered by 4 years ago

You can connect a MouseButton1Click event to every button in the same for loop that you use to create them. Like this:

for _,v in pairs(game.ReplicatedStorage.mythings:GetChildren()) do
    local button = Instance.new("TextButton", frame)
    button.Text = v.Name
    button.BackgroundColor3 = Color3.new(232, 67, 147)
    button.BorderColor3 = Color3.new(170, 255, 255)
    button.TextColor3 = Color3.new(255, 255, 255)
    button.TextSize = 14
    button.Size = UDim2.new(0, 146,0, 19)
    button.Font = "GothamBlack"
    button.TextScaled = true

    local buttonBG = Instance.new("TextButton", button)
    buttonBG.BackgroundColor3 = Color3.new(109, 110, 108)
    buttonBG.BorderColor3 = Color3.new(170, 255, 255)
    buttonBG.size = UDim2.new(1, 0,0.05, 0)
    buttonBG.Position = Vector2.new(0, 0, 1.02, 0)

    button.MouseButton1Click:Connect(function()
        -- change the value in the player's backpack here
        backpackValue.Value = button.Name
    end)
end
0
Ah, thank you :) NewGPU 36 — 4y
Ad

Answer this question