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)
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.
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.