Can't Find a User properly with the Function

been trying to get this script to check and remove the player’s template from the GUI whenever they left, but it returns a error.

local Decsendants = Folder:GetDescendants()

function PlayerLeft()
	for i, plr in pairs(Decsendants) do
		Folder:FindFirstChild(plr.Name):Destroy()
		print("Removed "..plr.Name.."'s Template Rank")
	end
end

here’s the error:

Players.iiNathxnism.PlayerGui.LeaderboardGUI.MainFrame.CustomLeaderboard.CustomLeaderboard:147: attempt to index nil with 'Destroy'

this is how my explorer looks like:
image

youre only getting the descendants of the folder once when the script runs, put the Decsendants variable inside of the PlayerLeft function

still returns the same error (char limit)

try this then:

function PlayerLeft()
	for i, plr in pairs(Decsendants) do
		if (Folder:FindFirstChild(plr.Name)) then 
			Folder[plr.Name]:Destroy()
			print("Removed "..plr.Name.."'s Template Rank")
		end
	end
end

I tried and it didn’t print the output, though I just noticed in my script, I was not using Descendants but using other variable or the previous variable Folder.

can you just provide the whole script would make it easier

local PlayerService		= game:GetService("Players")
local Folder					= script.Parent:WaitForChild("PlayerLists"):WaitForChild("Contents")
local Template				= Folder:WaitForChild("Template")
local GroupID				= 4399830

function UpdatePlayers()
	for i, plr in pairs(game.Players:GetPlayers()) do
		Template:Clone().Parent						= Folder
		local Court											= Template:WaitForChild("Court")
		
		-- // Diamonds
		if plr.Name	== "iiNathxnism" then
			Template.PlayerName.Text				=	"( "..plr.DisplayName.." ) @"..plr.Name
			Template.GroupRankNumber.Text		=	plr:GetRankInGroup(4399830)
			Template.GroupRankName.Text		=	plr:GetRoleInGroup(4399830)
			Template.CurrentLoc.Text					= "Testing"
			Template.CourtImage.Visible				= false
			Template.Name								= plr.Name
			Template.LayoutOrder						= 1
			Court.HalueveCourt.Visible				= true
			
			if plr.MembershipType == Enum.MembershipType.Premium then
				Template.PremiumMember.Visible= false
				Template.PremiumImage.Visible	= true
			else
				Template.PremiumMember.Visible= true
				Template.PremiumMember.Text	= "Not Premium"
				Template.PremiumImage.Visible	= false
			end
			if plr:IsFriendsWith(PlayerService.LocalPlayer.UserId) then
				Template.CreatorFriend.Text			= "Friend(s)"
			else
				Template.CreatorFriend.Text			= "Not Friend"
			end
		elseif plr.Name	== "SimpleMaker" then
			Template.PlayerName.Text				=	"( "..plr.DisplayName.." ) @"..plr.Name
			Template.GroupRankNumber.Text		=	plr:GetRankInGroup(4399830)
			Template.GroupRankName.Text		=	plr:GetRoleInGroup(4399830)
			Template.CurrentLoc.Text					= "Testing"
			Template.CourtImage.Visible				= false
			Template.Name								= plr.Name
			Template.LayoutOrder						= 1
			Court.EvaraCourt.Visible					= true

			if plr.MembershipType == Enum.MembershipType.Premium then
				Template.PremiumMember.Visible= false
				Template.PremiumImage.Visible	= true
			else
				Template.PremiumMember.Visible= true
				Template.PremiumMember.Text	= "Not Premium"
				Template.PremiumImage.Visible	= false
			end
			if plr:IsFriendsWith(PlayerService.LocalPlayer.UserId) then
				Template.CreatorFriend.Text			= "Friend(s)"
			else
				Template.CreatorFriend.Text			= "Not Friend"
			end
		elseif plr.Name	== "cosmiccatas" then
			Template.PlayerName.Text				=	"( "..plr.DisplayName.." ) @"..plr.Name
			Template.GroupRankNumber.Text		=	plr:GetRankInGroup(4399830)
			Template.GroupRankName.Text		=	plr:GetRoleInGroup(4399830)
			Template.CurrentLoc.Text					= "Testing"
			Template.CourtImage.Visible				= false
			Template.Name								= plr.Name
			Template.LayoutOrder						= 1
			Court.ElevesanCourt.Visible				= true

			if plr.MembershipType == Enum.MembershipType.Premium then
				Template.PremiumMember.Visible= false
				Template.PremiumImage.Visible	= true
			else
				Template.PremiumMember.Visible= true
				Template.PremiumMember.Text	= "Not Premium"
				Template.PremiumImage.Visible	= false
			end
			if plr:IsFriendsWith(PlayerService.LocalPlayer.UserId) then
				Template.CreatorFriend.Text			= "Friend(s)"
			else
				Template.CreatorFriend.Text			= "Not Friend"
			end
		elseif plr.Name	== "MysticPotato78" then
			Template.PlayerName.Text				=	"( "..plr.DisplayName.." ) @"..plr.Name
			Template.GroupRankNumber.Text		=	plr:GetRankInGroup(4399830)
			Template.GroupRankName.Text		=	plr:GetRoleInGroup(4399830)
			Template.CurrentLoc.Text					= "Testing"
			Template.CourtImage.Visible				= false
			Template.Name								= plr.Name
			Template.LayoutOrder						= 1
			Court.MiteraCourt.Visible					= true

			if plr.MembershipType == Enum.MembershipType.Premium then
				Template.PremiumMember.Visible= false
				Template.PremiumImage.Visible	= true
			else
				Template.PremiumMember.Visible= true
				Template.PremiumMember.Text	= "Not Premium"
				Template.PremiumImage.Visible	= false
			end
			if plr:IsFriendsWith(PlayerService.LocalPlayer.UserId) then
				Template.CreatorFriend.Text			= "Friend(s)"
			else
				Template.CreatorFriend.Text			= "Not Friend"
			end
			-- Diamond Pearls
			
			
			-- Normal Users
		else
			Template.PlayerName.Text				=	"( "..plr.DisplayName.." ) @"..plr.Name
			Template.GroupRankNumber.Text		=	plr:GetRankInGroup(4399830)
			Template.GroupRankName.Text		=	plr:GetRoleInGroup(4399830)
			Template.CurrentLoc.Text					= "Testing"
			Template.Name								= plr.Name
			Template.LayoutOrder						= 2
			if plr:GetRankInGroup(GroupID) == 150 then
				Court.HalueveCourt.Visible				= true
			elseif plr:GetRankInGroup(GroupID) == 151 then
				Court.MiteraCourt.Visible					= true
			elseif plr:GetRankInGroup(GroupID) == 152 then
				Court.EvaraCourt.Visible					= true
			elseif plr:GetRankInGroup(GroupID) == 153 then
				Court.ElevesanCourt.Visible				= true
			else
				Court.Visible										= false
				Template.CourtImage.Text				= "Not in Court!"
			end		
			if plr.MembershipType == Enum.MembershipType.Premium then
				Template.PremiumMember.Visible= false
				Template.PremiumImage.Visible	= true
			else
				Template.PremiumMember.Visible= false
				Template.PremiumImage.Visible	= false
			end
			if plr:IsFriendsWith(PlayerService.LocalPlayer.UserId) then
				Template.CreatorFriend.Text			= "Friend(s)"
			else
				Template.CreatorFriend.Text			= "Not Friend"
			end
		end
	end
end

function PlayerLeft()
	local Decsendants = Folder:GetDescendants()
	for i, plr in pairs(Decsendants) do
		if Decsendants[plr.Name] then 
			Folder[plr.Name]:Destroy()
			print("Removed "..plr.Name.."'s Template Rank")
		end
	end
end

--[[function PlayerLeft()
	local Decsendants = Folder:GetDescendants()
	for i, plr in pairs(Decsendants) do
		if Decsendants[plr.Name] then
			Decsendants[plr.Name]:Destroy()
			warn("Removed "..plr.Name.."'s Template Rank")
		end
	end
end]]

UpdatePlayers()
PlayerService.PlayerAdded:Connect(UpdatePlayers)
PlayerService.PlayerRemoving:Connect(PlayerLeft)

I think its unnecessary to have the playerJoined function be in this script- just a thought since I’m mostly having problems with the PlayerLeft.

you can just pass the player that is leaving to the PlayerLeft function and check if a frame with the same name as the player exists

local PlayerService		= game:GetService("Players")
local Folder					= script.Parent:WaitForChild("PlayerLists"):WaitForChild("Contents")
local Template				= Folder:WaitForChild("Template")
local GroupID				= 4399830

function UpdatePlayers()
	for i, plr in pairs(game.Players:GetPlayers()) do
		Template:Clone().Parent						= Folder
		local Court											= Template:WaitForChild("Court")

		-- // Diamonds
		if plr.Name	== "iiNathxnism" then
			Template.PlayerName.Text				=	"( "..plr.DisplayName.." ) @"..plr.Name
			Template.GroupRankNumber.Text		=	plr:GetRankInGroup(4399830)
			Template.GroupRankName.Text		=	plr:GetRoleInGroup(4399830)
			Template.CurrentLoc.Text					= "Testing"
			Template.CourtImage.Visible				= false
			Template.Name								= plr.Name
			Template.LayoutOrder						= 1
			Court.HalueveCourt.Visible				= true

			if plr.MembershipType == Enum.MembershipType.Premium then
				Template.PremiumMember.Visible= false
				Template.PremiumImage.Visible	= true
			else
				Template.PremiumMember.Visible= true
				Template.PremiumMember.Text	= "Not Premium"
				Template.PremiumImage.Visible	= false
			end
			if plr:IsFriendsWith(PlayerService.LocalPlayer.UserId) then
				Template.CreatorFriend.Text			= "Friend(s)"
			else
				Template.CreatorFriend.Text			= "Not Friend"
			end
		elseif plr.Name	== "SimpleMaker" then
			Template.PlayerName.Text				=	"( "..plr.DisplayName.." ) @"..plr.Name
			Template.GroupRankNumber.Text		=	plr:GetRankInGroup(4399830)
			Template.GroupRankName.Text		=	plr:GetRoleInGroup(4399830)
			Template.CurrentLoc.Text					= "Testing"
			Template.CourtImage.Visible				= false
			Template.Name								= plr.Name
			Template.LayoutOrder						= 1
			Court.EvaraCourt.Visible					= true

			if plr.MembershipType == Enum.MembershipType.Premium then
				Template.PremiumMember.Visible= false
				Template.PremiumImage.Visible	= true
			else
				Template.PremiumMember.Visible= true
				Template.PremiumMember.Text	= "Not Premium"
				Template.PremiumImage.Visible	= false
			end
			if plr:IsFriendsWith(PlayerService.LocalPlayer.UserId) then
				Template.CreatorFriend.Text			= "Friend(s)"
			else
				Template.CreatorFriend.Text			= "Not Friend"
			end
		elseif plr.Name	== "cosmiccatas" then
			Template.PlayerName.Text				=	"( "..plr.DisplayName.." ) @"..plr.Name
			Template.GroupRankNumber.Text		=	plr:GetRankInGroup(4399830)
			Template.GroupRankName.Text		=	plr:GetRoleInGroup(4399830)
			Template.CurrentLoc.Text					= "Testing"
			Template.CourtImage.Visible				= false
			Template.Name								= plr.Name
			Template.LayoutOrder						= 1
			Court.ElevesanCourt.Visible				= true

			if plr.MembershipType == Enum.MembershipType.Premium then
				Template.PremiumMember.Visible= false
				Template.PremiumImage.Visible	= true
			else
				Template.PremiumMember.Visible= true
				Template.PremiumMember.Text	= "Not Premium"
				Template.PremiumImage.Visible	= false
			end
			if plr:IsFriendsWith(PlayerService.LocalPlayer.UserId) then
				Template.CreatorFriend.Text			= "Friend(s)"
			else
				Template.CreatorFriend.Text			= "Not Friend"
			end
		elseif plr.Name	== "MysticPotato78" then
			Template.PlayerName.Text				=	"( "..plr.DisplayName.." ) @"..plr.Name
			Template.GroupRankNumber.Text		=	plr:GetRankInGroup(4399830)
			Template.GroupRankName.Text		=	plr:GetRoleInGroup(4399830)
			Template.CurrentLoc.Text					= "Testing"
			Template.CourtImage.Visible				= false
			Template.Name								= plr.Name
			Template.LayoutOrder						= 1
			Court.MiteraCourt.Visible					= true

			if plr.MembershipType == Enum.MembershipType.Premium then
				Template.PremiumMember.Visible= false
				Template.PremiumImage.Visible	= true
			else
				Template.PremiumMember.Visible= true
				Template.PremiumMember.Text	= "Not Premium"
				Template.PremiumImage.Visible	= false
			end
			if plr:IsFriendsWith(PlayerService.LocalPlayer.UserId) then
				Template.CreatorFriend.Text			= "Friend(s)"
			else
				Template.CreatorFriend.Text			= "Not Friend"
			end
			-- Diamond Pearls


			-- Normal Users
		else
			Template.PlayerName.Text				=	"( "..plr.DisplayName.." ) @"..plr.Name
			Template.GroupRankNumber.Text		=	plr:GetRankInGroup(4399830)
			Template.GroupRankName.Text		=	plr:GetRoleInGroup(4399830)
			Template.CurrentLoc.Text					= "Testing"
			Template.Name								= plr.Name
			Template.LayoutOrder						= 2
			if plr:GetRankInGroup(GroupID) == 150 then
				Court.HalueveCourt.Visible				= true
			elseif plr:GetRankInGroup(GroupID) == 151 then
				Court.MiteraCourt.Visible					= true
			elseif plr:GetRankInGroup(GroupID) == 152 then
				Court.EvaraCourt.Visible					= true
			elseif plr:GetRankInGroup(GroupID) == 153 then
				Court.ElevesanCourt.Visible				= true
			else
				Court.Visible										= false
				Template.CourtImage.Text				= "Not in Court!"
			end		
			if plr.MembershipType == Enum.MembershipType.Premium then
				Template.PremiumMember.Visible= false
				Template.PremiumImage.Visible	= true
			else
				Template.PremiumMember.Visible= false
				Template.PremiumImage.Visible	= false
			end
			if plr:IsFriendsWith(PlayerService.LocalPlayer.UserId) then
				Template.CreatorFriend.Text			= "Friend(s)"
			else
				Template.CreatorFriend.Text			= "Not Friend"
			end
		end
	end
end

function PlayerLeft(LeavingPlayer)
	local PlayerFrame = Folder:FindFirstChild(LeavingPlayer.Name)
	
	if (PlayerFrame ~= nil) then 
		PlayerFrame:Destroy()
	end
end

--[[function PlayerLeft()
	local Decsendants = Folder:GetDescendants()
	for i, plr in pairs(Decsendants) do
		if Decsendants[plr.Name] then
			Decsendants[plr.Name]:Destroy()
			warn("Removed "..plr.Name.."'s Template Rank")
		end
	end
end]]

UpdatePlayers()
PlayerService.PlayerAdded:Connect(UpdatePlayers)
PlayerService.PlayerRemoving:Connect(PlayerLeft)

wow that’s the most easiest fix :sob: it works, the print is working properly now I just need to test this out with a live-player in a game, Thanks @CZXPEK ! I’ll make sure to tweak it more a bit later on.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.