How to detect loading orb for respawned players?

Hey guys, I can detect when a player just started a game and do some actions when a protective orb has gone,

local Players = game:GetService("Players")
	for _, player in pairs(Players:GetPlayers()) do
		-- this code will run for all players
		Main_CHR_name = player.Name
		break -- I got only one player for this epesode
	end
Main_CHR = workspace:FindFirstChild(Main_CHR_name)
	if RunService:IsStudio() == false then
		-- games only
		local the_orb = "xxx" -- what ever, but not nil
		while the_orb ~= nil do
			the_orb = Main_CHR:FindFirstChild("ForceField")
			-- the_orb = Main_CHR:FindFirstChildOfClass("ForceField")
-- a line above is same thing
			if not the_orb then
				print("orb's gone")
				break
			else
				print("waiting...")
				wait(0.1)
			end
		end
	end

The code works perfectly for those who just joined a game, but gives an error if a player has respawned. How to fix it?

1 Like

May I ask what are you trying to do here?

Try this:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		
		repeat print("Waiting") task.wait() until (not char:FindFirstChildWhichIsA("ForceField"))
		print("Gone")
		
	end)
end)

In my game a player first get to a “lobby”, from which it can join a level of a game. But I do not want him to enter a level with the protective orb on. Somehow I have to detect it.

Unfortunately works in the same manner: only for the start and never for a respawn… same err…
attempt to index nil with ‘FindFirstChildWhichIsA’
The Orb is present though on the player…

I think there’s a ForceField property called “Duration” that you can set to “0” to completely remove the forcefield that spawns around you when you join. (It’s in the SpawnLocation)

Works perfectly for me, also char shouldn’t be nil because it waits for the character to spawn in

I do not need to set it to zero, it is 10 by default and on purpose.

Maybe where you get spawned in after you select the game, you can do:

if(game.Players.LocalPlayer.Character:FindFirstChild("ForceField")) then

That should detect whether the player has a forcefield or not.

local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		while task.wait() do
			if character:FindFirstChild("ForceField") then
				print(player.Name, " has a forcefield!")
				--run code while they have forcefield
			else
				print(player.Name, " has lost their forcefield!")
				--run code when the forcefield first ends
				break --break out of loop if you dont need to check if player doesnt have forcefield
			end
		end
	end)
end)

You don’t need a script this complicated to achieve this.

2 Likes

I was catching character externally, not via :Connect, that is why it worked at start only and crashed at respawns.
Thanks!