Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

MouseButton1Click on new instance?

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

✓ Accepted answer

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

1 comment

0

Oh thanks! I didn't think about that.

This discussion is preserved as it was. New answers, comments, and votes are closed.