Checking for a GUI in PlayerGui doesn't work?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

Have players who don’t have the welcome GUI don’t get teleported into the game.

  1. What is the issue?

I still get teleported with the Gui visible, it prints the statement that it doesn’t exist in the PlayerGui

  1. What solutions have you tried so far?

I tried making a method where it used a RemoteEvent but this didn’t work out.

  1. Current code for my GUI check (doesn’t work)
local function teleportOnSpawn(players, spawns)
	for _, player in ipairs(players:GetPlayers()) do
		if player.PlayerGui:FindFirstChild("Welcome") then
			print("Player has GUI, not teleporting")
			-- Skip teleportation for players with the GUI
		else
			print("No GUI for player, teleporting") -- I get this statement with the GUI in PlayerGui

			local character = player.Character
			if character then
				local HRP = character:FindFirstChild("HumanoidRootPart")
				if HRP then
					local spawn = spawns[math.random(1, #spawns)]
					HRP.CFrame = spawn.CFrame + Vector3.new(0, 5, 0)
					print("Teleported " .. player.Name .. " to " .. spawn.Name)
				end
			end
		end
	end
end

Just curious, is there a chance that you have your GUI inside of a ScreenGUI, or another container object? So…

if player.PlayerGui.ScreenGui:FindFirstChild(...)

I would also be sure to check what is calling the function, and it actually is either the Players Service or the service defined correctly as a variable.

You could always add this statement…

for _, player in ipairs(players:GetPlayers()) do
        print(player.PlayerGui) -- or player.PlayerGui:GetChildren() to test what the contents are
		if player.PlayerGui:FindFirstChild("Welcome") then

There’s also a chance that the player isn’t loaded in when the teleportOnSpawn() function is called, which could cause some issues.

Honestly, it is somewhat hard to understand what you mean by “Have players who don’t have the welcome GUI don’t get teleported into the game.”

So maybe just debug with some print statements, check your references on directories such as PlayerGui or add some additional context, maybe the reference to the teleportOnSpawn() function so we can see how it is called.

I meant it as, when the player is still in the title menu that it wouldn’t teleport him/her.

I also just realised that the issue lies that the client side clones the GUI into the PlayerGui, how would I go about fixing this. Just use a remote event that returns a boolean?

ps: sorry for the late reply!

Is the original code in your post a ServerScript? Can I see the client-side code as well? if teleportOnSpawn is linked to Players.PlayerAdded:Connect(teleportOnSpawn) then perhaps that function is being ran before the GUI cloning. Because even if it is a clone, if you set the name to “Welcome”, your teleportOnSpawn() function doesn’t look inherently wrong to me. Keep in mind I am no expert, just trying to help.

But yes, I assume you could just have a remote event fired from the Client Side that looks in the PlayerGui for your GUI and just send true/false to decide if the player gets teleported, but you would have to get that variable into the teleportOnSpawn(players, spawns, haveGui : bool) function , which might also be somewhat hard to sync up depending on what is calling it on the Server.