Crime Report Script not Working Properly

I have a script that allows players to report another player for committing a crime in game. The problem is that one of the buttons doesn’t work. For example, if there are 4 buttons, only 3 would work. If there were 2, only 1 would work. It seems it is always the second to last player to join whose button isn’t working.

Script:

local Players = game:GetService("Players")
local Label = script.Parent.Parent:FindFirstChild("Player")
local Selected = script.Parent:FindFirstChild("Player")
local Button = script.Parent.Parent.TextButton
local thumbType = Enum.ThumbnailType.HeadShot
local thumbSize = Enum.ThumbnailSize.Size420x420
local P = game.Workspace.Posters:GetChildren()
local Image = script.Parent.Parent:FindFirstChild("ImageLabel")

Players.PlayerAdded:Connect(function(Player)
	local Button = Instance.new("TextButton")
	Button.Name = Player.Name
	Button.Size = UDim2.new(1, 0, 0, 50)
	Button.Font = Enum.Font.Oswald
	Button.Text = Player.Name
	Button.TextScaled = true
	Button.Parent = script.Parent
	Button.BackgroundColor3 = Color3.fromRGB(0, 153, 0)
end)

Players.PlayerRemoving:Connect(function(Player)
	local Button = script.Parent:FindFirstChild(Player.Name)
	Button:Destroy()
	if Label.Text == Player.Name then
		Label.Text = "Player:"
	end
	if Selected.Value == Player.Name then
		Selected.Value = ""
	end
end)

Button.MouseButton1Click:Connect(function()
	if Selected.Value ~= "" then
		local userId = Players:FindFirstChild(Selected.Value).UserId
		local content, isReady = Players:GetUserThumbnailAsync(userId, thumbType, thumbSize)
		for i = 1,#P do
			local SGUI = P[i]:FindFirstChild("SurfaceGui")
			local imageLabel = SGUI:FindFirstChild("ImageLabel")
			imageLabel.Image = content
		end
	end
end)

function ResetButtons()
	local C = script.Parent:GetChildren()
	for i = 1, #C do
		if C[i]:IsA("TextButton") then
			C[i].MouseButton1Click:Connect(function()
				local userId = Players:FindFirstChild(C[i].Name).UserId
				if Label.Text == C[i].Name then
					Label.Text = "Player:"
					Selected.Value = ""
					Image.Image = ""
				else
					Label.Text = C[i].Name
					Selected.Value = C[i].Name
					local content, isReady = Players:GetUserThumbnailAsync(userId, thumbType, thumbSize)
					Image.Image = content
				end
			end)
		end
	end
end

script.Parent.ChildRemoved:Connect(function()
	ResetButtons()
end)

script.Parent.ChildAdded:Connect(function()
	ResetButtons()
end)

Is this in a local script? if so, you should fire an event on the server to setup the buttons, and whoever leaves, fire all clients the button to remove.

It’s inside of a SurfaceGui, not local.

I have it fixed now, I had to remove the part that makes it where if you press the button of a player whose is already on the screen it goes away.