I have this player list which works perfectly fine,
the issue
I’m trying to figure out how to destroy the player’s textbutton when they leave.
Here’s the code: ( I deleted some other code to make the problem easier to solve )
Server:
local module = require(game.ReplicatedStorage.ModuleScript);
local rep = game:GetService("ReplicatedStorage")
local ev = rep:FindFirstChild("RemoteEvent");
game.Players.PlayerAdded:Connect(function(plr)
ev:FireAllClients(plr)
module.makeListedStarter(plr);
end)
Module script
local module = {}
module.makeListedPlayer = function(plr)
local playerService = game:GetService("Players");
local localPlayer = playerService.LocalPlayer;
local playerGui = localPlayer.PlayerGui;
local playerList = playerGui.list.Frame;
local frameInstance = Instance.new("TextButton");
frameInstance.BackgroundColor3 = Color3.fromRGB(35, 35, 35);
frameInstance.Size = UDim2.new(0.998, 0,0.095, 0);
frameInstance.Parent = playerList;
frameInstance.Name = plr.Name;
frameInstance.Text = plr.Name;
frameInstance.TextColor3 = Color3.fromRGB(255,255,255);
frameInstance.BorderSizePixel = 0;
frameInstance.AutoButtonColor = false
frameInstance.MouseEnter:Connect(function()
frameInstance.BackgroundColor3 = Color3.fromRGB(63,63,63)
end)
frameInstance.MouseLeave:Connect(function()
frameInstance.BackgroundColor3 = Color3.fromRGB(35,35,35)
end)
local cons = Instance.new("UITextSizeConstraint");
cons.Parent = frameInstance;
cons.MaxTextSize = 14;
cons.MinTextSize = 1;
end
module.makeListedStarter = function(plr)
local starterGui = game:GetService("StarterGui");
local plist = starterGui.list;
local frame = plist.Frame
local frameInstance = Instance.new("TextLabel");
frameInstance.BackgroundColor3 = Color3.fromRGB(65, 65, 98);
frameInstance.Size = UDim2.new(0.998, 0,0.095,0);
frameInstance.Parent = frame;
frameInstance.Name = plr.Name;
frameInstance.Text = plr.Name;
frameInstance.TextColor3 = Color3.fromRGB(255,255,255);
frameInstance.BorderSizePixel = 0;
local cons = Instance.new("UITextSizeConstraint");
cons.Parent = frameInstance;
cons.MaxTextSize = 14;
cons.MinTextSize = 1;
end
return module;
Local script:
local module = require(game.ReplicatedStorage.ModuleScript);
local rep = game:GetService("ReplicatedStorage");
local ev = rep:FindFirstChild("RemoteEvent");
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, false);
ev.OnClientEvent:Connect(function(plr)
module.makeListedPlayer(plr);
end)