Loop decides to skip the rest after outputting an error in a pcall

So for this specific example, the players are being teamed. Sometimes, when a player leaves during this session, it will trigger an error to occur. So I decided to add a pcall to stop it from breaking. So it does, and continues onto the next thing. However, it doesn’t team the rest of the players.

5 out of 20 players get teamed. The 6th player leaves and triggers the error. It then skips the rest of the players to the team and continues to the next thing.

How can I solve this?

for i,v in pairs(game:GetService("Players"):GetPlayers() do
        if v == nil then return end
        local success, err = pcall(function()
	       v.Team = game:GetService("Teams"):FindFirstChild("Spectator")
1 Like

I think you can sort the players that didn’t leave and add them to another table. From that table, you team them up.

1 Like

Pretty sure the issue is that you have “if v == nil then return end.” The thing is, if the pcall is going to error, i’d believe it’s because v is nil (I think. If a player leaves the game, wouldn’t the instance become nil?). Therefore, by doing return end, you’re stopping the loop and making the script move on to the next thing. I’d simply remove “if v == nil,” and also to clean up, you can delete “local success, err =” since you’re not using those variables.

1 Like

Still occurs when I remove the “if v == nil then return end” It won’t change anything.

Return means you skip over the entire loop. Use continue instead.

1 Like

Ye, even when I remove the return, it still skips the rest of the players.

I’d find it weird if it continued to do that even after you removed the “if v == nil.” That’s the only line in your script that would cause the script to exit the function. :thinking:

Perhaps you could use ipairs()? That should automatically skip over anything that’s nil.

1 Like

Could server lag also contribute to this?

Hmm, I wouldn’t see anything in this script that server lag would affect (other than slowing down the loop by a smudge). If your server does happen to be lagging, though, I would change your script to define local Teams = game:GetService("Teams"); local Spectator = Teams:WaitForChild("Spectator") at the very start of the script instead of doing it each time the script iterates in the pairs() loop.

But yeah, I dont think server lag would contribute to the problem you describes.

1 Like
  1. Why are you pcalling setting the team property? This shouldn’t be erroring if your team is already present. And your team should already be present, so you shouldn’t be using FindFirstChild.

  2. Can you show the full error trace? The post doesn’t have enough information about what the actual error is.

There should be no error trace if he’s wrapping it in a protected call and not throwing the error.

Umm, I might be wrong but I’ve noticed about your code. the part from line 1, it’s missing a closing “)”.

I don’t know if I’m wrong or maybe you paste there wrong. ¯_(ツ)_/¯

I believe this is the fix you need:

local Players = game:GetService("Players")
local Teams = game:GetService("Teams")

for _, player in ipairs(Players:GetPlayers()) do
    -- If for some reason the player doesn't exist anymore?
	if player.Character then
		player.Team = Teams.Spectator

If the player has disconnected, that player’s Character property will be nil.

The OP is titled “after outputting an error in a pcall.” It’s a fair assumption that an error was outputted.

Before I added a pcall, it would sometimes trigger an error while a player leaves the game indexing that .Team is nil. However, once I put it into a pcall, the error no longer broke the code.

pcall just suppresses errors, but used like you did in your first post isn’t good. Suppressing errors without any trace of them isn’t the best. Have you tried the solution I’ve posted?

If you wish to read about the error and talk about the pcall, please do it here: Failed because player left game

Okay, now I see. Checking v ~= nil won’t solve it because v will always be a truthy value (the Player object). You could try to check if not v.Parent then continue end and that might work better.

1 Like