Help on my kick panel

Hi, Im trying to make a kick panel, and it has a playerlist, but whenever the player gets kicked they dont get removed from the list.
heres my script:

local players = game:GetService("Players")
local playerlist = players:GetPlayers()
local username = script.Parent.Parent.Player

for _, player in pairs(playerlist)do
	playerName = player.Name
	local template = Instance.new("TextButton")
	template.Name = playerName
	template.BorderSizePixel = 0
	template.Font = Enum.Font.SourceSansBold
	template.Parent = script.Parent
	template.Size = UDim2.new(0, 319,0, 28)
	template.BackgroundColor3 = Color3.new(0.368627, 0.368627, 0.368627)
	template.Text = playerName
	template.TextColor3 = Color3.new(1, 1, 1)
	template.TextScaled = true
	template.MouseButton1Click:Connect(function()
	username.Text = template.Name
end)
end

Instead of showing the playerlist script, shouldn’t you show the kick panel script?

1 Like
local frame = script.Parent

frame.Cancel.MouseButton1Click:Connect(function()
	frame.Visible = false
end)

frame.Kick.MouseButton1Click:Connect(function()
	if game.Players:FindFirstChild(frame.Player.Text) then
		game.ReplicatedStorage.KickPlayer:FireServer(frame.Player.Text, frame.Reason.Text)
	end
end)

Theres a kickhandler in serverscriptservice

game.ReplicatedStorage.KickPlayer.OnServerEvent:Connect(function(player, playertokick, reason)
	game.Players:FindFirstChild(playertokick):Kick(reason)
end)

I’m not that great of a scripter, but I think you should add code that deletes [kicked_player_username] from the playerlist.

1 Like

Where would I put that script? in the for loop?

Most probably. I THINK thats where it would go (In the playerlist script).

Ok so I did it.

local players = game:GetService("Players")
local playerlist = players:GetPlayers()
local username = script.Parent.Parent.Player

for _, player in pairs(playerlist)do
	playerName = player.Name
	local template = Instance.new("TextButton")
	template.Name = playerName
	template.BorderSizePixel = 0
	template.Font = Enum.Font.SourceSansBold
	template.Parent = script.Parent
	template.Size = UDim2.new(0, 319,0, 28)
	template.BackgroundColor3 = Color3.new(0.368627, 0.368627, 0.368627)
	template.Text = playerName
	template.TextColor3 = Color3.new(1, 1, 1)
	template.TextScaled = true
	template.MouseButton1Click:Connect(function()
	username.Text = template.Name
	game.Players.PlayerRemoving:Connect(function(PlayerRemoved)
	if template.Name == PlayerRemoved.Name then
		template:Destroy()
	end	
end)
end)
end

Im gonna test it now.

On player1’s screen player2 doesnt show.


but on player2’s screen player1 shows.

Because the player joined after the script already ran. You should add a PlayerAdded event listener

1 Like

Could you show me what to write?

game:GetService'Players'.PlayerAdded:Connect(function(Player)
    -- do stuff to the Player that just joined
end)

Link it to a PlayerAdded to add players when they joined to add a player to the list and then a PlayerRemoving when they leave the game to remove them from the list

Just update the list with those two events

What would I do in the script?

local function update()
        for i, player in pairs(game.Players:GetPlayers())
      playerName = player.Name
	local template = Instance.new("TextButton")
	template.Name = playerName
	template.BorderSizePixel = 0
	template.Font = Enum.Font.SourceSansBold
	template.Parent = script.Parent
	template.Size = UDim2.new(0, 319,0, 28)
	template.BackgroundColor3 = Color3.new(0.368627, 0.368627, 0.368627)
	template.Text = playerName
	template.TextColor3 = Color3.new(1, 1, 1)
	template.TextScaled = true
	template.MouseButton1Click:Connect(function()
	username.Text = template.Name
        end
end
game.Players.PlayerAdded:Connect(update)
game.Players.PlayerRemoving:Connect(update)

it just updates everytime the player count changes

1 Like

image

local Players = game:GetService("Players")
local username = script.Parent.Parent.Player

local function OnPlayer(Player)
	local template = Instance.new("TextButton")
	template.Name = Player.Name
	template.BorderSizePixel = 0
	template.Font = Enum.Font.SourceSansBold
	template.Parent = script.Parent
	template.Size = UDim2.new(0, 319,0, 28)
	template.BackgroundColor3 = Color3.new(0.368627, 0.368627, 0.368627)
	template.Text = Player.Name
	template.TextColor3 = Color3.new(1, 1, 1)
	template.TextScaled = true
	template.MouseButton1Click:Connect(function()
		username.Text = template.Name
	end)
end

-- run OnPlayer function on everyone already in game
for _, Player in pairs(Players:GetPlayers())do
	OnPlayer(Player)
end

-- and also run it on players who join the game later
Players.PlayerAdded:Connect(OnPlayer)

-- on player removing
Players.PlayerRemoving:Connect(function(Player)
	-- try to find the template with the name of the player who is getting removed
	local template = script.Parent:FindFirstChild(Player.Name)
	-- if template exists then delete template
	if template then
		template:Destroy()
	end	
end)
1 Like

I edited the script I forgot to add a loop

1 Like