Error When Player leaves

I have an error in which when A Gui that all the desktops are full and the player with that Gui set Exits the game, this function of “if not player.PlayerGui: FindFirstChild (” DeskFull “) then” does not work, it gives me this mistake, my game is called Office simulator, Note : I am not good explaining

Script:

local DeskFolder = Instance.new("Folder",workspace)
DeskFolder.Name = "Desks"


game.Players.PlayerAdded:Connect(function(player)
	
	local new_computer = ComputerModel:Clone()
	local desk_position = nil
	
	
	for i,v in ipairs(DeskPositions:GetChildren()) do
		if v:GetAttribute("Occupied") == false then
			
			v:SetAttribute("Occupied",true)
			desk_position = v.Position
			local desk_pos_part = Instance.new("ObjectValue",player)
			desk_pos_part.Name = "DeskPositionPart"
			desk_pos_part.Value = v
			print(v)
			break
			
		elseif v:GetAttribute("Occupied") == true then
			print("All Desks Occupied!")
			if not player.PlayerGui:FindFirstChild("DeskFull") then
				print("gived gui")
				local clone = deskfullGui:Clone()
				clone.Parent = player.PlayerGui
			end
		end
		
	end
	
	new_computer:SetPrimaryPartCFrame(CFrame.new(desk_position + Vector3.new(1.5,1,0))) -- Es 0,1,0
	new_computer.PrimaryPart.Orientation = Vector3.new(0,90,0)
	new_computer.Parent = DeskFolder
	new_computer.Name = "Desk ".. player.Name
	wait(0.1)
	player.Character.Torso.CFrame = CFrame.new(desk_position)

end)
	
game.Players.PlayerRemoving:Connect(function(player)
	print("Leaved With No GUI")  -- there gives me the error
	 if not player.PlayerGui:FindFirstChild("DeskFull") then
	    player.DeskPositionPart:SetAttribute("Occupied",false)
		DeskFolder["Desk "..player.Name]:Destroy()
	end
	
end)				

Error Photo:

Additional photos :

image
image


Why does this happens?

try using player:WaitForChild(“PlayerGui”) and tell me if you get a error or Infinite Yield warning.

1 Like

Seconding the above post, try “WaitForChild” on the PlayerGui.

if not player:WaitForChild("PlayerGui"):WaitForChild("DeskFull") then

I think it may be better for them to use :FindFirstChild(“DeskFull”) because :WaitForChild(“DeskFull”) will return an error if it doesn’t exist.

i dont know if it worked, it doesnt prints anything, it should print “not desk affected” because on this test i did, i didnt was with the gui
image

Weird so PlayerGui just doesn’t exist whenever they leave? Try relaunching studio.

I am very sure its not my studio, never happened this before

try game.Players[player.Name].PlayerGui

I don’t know if it would change anything but yk worth a try.

stills gives me this error
image

That’s really weird hold on lemme try this in my studio on a test game real quick i’ll be right back.

1 Like

I don’t have this error when using something like this? Are you sure it’s not your studio?

1 Like

Yes i am sure, also my internet is good

Ok then try adding a pcall to this.

local sucess, err = pcall(function()
  --Code Goes Here--
end)

if sucess then
  print("Worked!")
else
  warn(err)
end
1 Like

Inside of the game.Players.PlayerRemoving function btw @SanJoseYTXD

1 Like

Try to test this in roblox and not in studio, roblox studio closes all the things faster than normal roblox, so maybe the script does not have time to get the PlayerGui from the player.

After testing your code in my place, I managed to fix it:

local DeskFolder = Instance.new("Folder",workspace)
DeskFolder.Name = "Desks"


game.Players.PlayerAdded:Connect(function(player)

	local new_computer = ComputerModel:Clone()
	local desk_position = nil


	for i,v in ipairs(DeskPositions:GetChildren()) do
		if v:GetAttribute("Occupied") == false then

			v:SetAttribute("Occupied",true)
			desk_position = v.Position
			local desk_pos_part = Instance.new("ObjectValue",player)
			desk_pos_part.Name = "DeskPositionPart"
			desk_pos_part.Value = v
			print(v)
			break

		elseif v:GetAttribute("Occupied") == true then
			print("All Desks Occupied!")
			if not player.PlayerGui:FindFirstChild("DeskFull") then
				print("gived gui")
				local clone = deskfullGui:Clone()
				clone.Parent = player.PlayerGui
			end
		end

	end

	new_computer:SetPrimaryPartCFrame(CFrame.new(desk_position + Vector3.new(1.5,1,0))) -- Es 0,1,0
	new_computer.PrimaryPart.Orientation = Vector3.new(0,90,0)
	new_computer.Parent = DeskFolder
	new_computer.Name = "Desk ".. player.Name
	wait(0.1)
	player.Character.Torso.CFrame = CFrame.new(desk_position)

end)

game.Players.PlayerRemoving:Connect(function(player)
	print("Leaved With No GUI")  -- there gives me the error
	
	local PlayerGui = player:FindFirstChild("PlayerGui")
	local Gui = PlayerGui:FindFirstChild("DeskFull")
	
	if Gui == nil then
		player.DeskPositionPart:SetAttribute("Occupied",false)
		DeskFolder["Desk "..player.Name]:Destroy()
	end
end)		

I hope i helped you to solve the problem, if so, be sure to mark my reply as a solution.

Happy coding!

2 Likes

It worked, thank you guys for helping!!.

2 Likes

Both will return an error if DeskFull doesn’t exist, one will just say “infinite yield possible” other will be “playergui has no member/child named” etc. Weird how PlayerGui isn’t a valid member of the Player when they are leaving.

1 Like

I am not sure if is because the way i am testing, i am testing it on this method:

I Play it, then delete my player in Players

As you said, it gives me this error

image

local PlayerGui = player:FindFirstChild("PlayerGui")
local Gui = player:FindFirstChild("DeskFull")

This is also wrong, DeskFull is supposed to be a child of PlayerGui not the Player instance itself.