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

MouseButton1Click on new instance?

Asked by 6 years ago

So I'm attempting to make an Admin Panel but I ran into a problem. I use Instance.new() to create a new TextButton when a player is added into the game. How would I use MouseButton1Click for each instance to click the new text button that was created?

This is my update function to add the text buttons into the panel.

function update()
    for i,v in pairs(game.Players:GetChildren())do
        if playerList:FindFirstChild(v.Name) then playerList[v.Name]:Destroy() else
            local newPlayer = Instance.new("TextButton")
            newPlayer.Name = v.Name
            newPlayer.Text = v.Name
            newPlayer.TextScaled = true
            newPlayer.Parent = playerList
        end
    end
end

When a player is added into the game it adds the text button like this. Here!

Simpled down: Use MouseButton1Click on a new Text Button that was created using Instance.new()

1 answer

Log in to vote
0
Answered by 6 years ago

Connect a MouseButton1Click Event after you created the instance. The script should now look like this:

function update()
    for i,v in pairs(game.Players:GetChildren())do
        if playerList:FindFirstChild(v.Name) then playerList[v.Name]:Destroy() else
            local newPlayer = Instance.new("TextButton")
            newPlayer.Name = v.Name
            newPlayer.Text = v.Name
            newPlayer.TextScaled = true
            newPlayer.Parent = playerList
            newPlayer.MouseButton1Click:Connect(function()
                   -- Your code here
            end)             
        end
    end
end
0
Oh thanks! I didn't think about that. Sharkeedub 179 — 6y
Ad

Answer this question