For i, v in pairs(game.Players:GetPlayers()) only works for one person

Trust me, that isn’t really important, and nothing is in the output. Positive.

If you’re 100% sure nothing is in the output regarding errors or printing “a”, you are likely yielding the loop with your timer() function. I cant offer you a solution because I don’t know what the timer function does unfortunately.

If you do not wish to share the timer function, please try changing:
timer()
to
spawn(timer)

	local function timer()
	for i = currenttimer, 0, -1 do
		wait(1)
		print(i)
		if i == 0 then
			for i, v in pairs(game.Players:GetPlayers()) do
				ingame = false
				if game.ReplicatedStorage.PlayerInGame:FindFirstChild(v.Name) then
			v.Character.Humanoid.Health = 0
		game.ReplicatedStorage.PlayerInGame:FindFirstChild(v.Name):Destroy()
		game.ReplicatedStorage.Bindables.LoseBindable:Invoke(v)
				end
			end
		elseif ingame == false then
			break
			end
	end
	end

Pretty sure the “for” gets the results once after the script runs, and doesn’t fetch new results after.

Ah yes, there is your issue.

When you call the timer function, its a loop which counts down from a certain number. When you call a function Lua doesn’t set it to a new thread or coroutine. It just runs inside the current thread meaning the function you called it from has to wait until that function is finished.

To fix this problem, assuming you want this to run for each player, you need to create a new coroutine for this loop to run on. You can do this by using the coroutine functions such as coroutine.create(), but for this case spawn should work well.

Try changing timer() to spawn(timer)

This will create a new thread for the loop to run on, I’d recommend reading up on the function just to get a better understanding of how it works.

IF YOU ONLY WANT THE TIMER FUNCTION TO RUN ONCE… Run the timer function at the end of the for loop (AKA after the end which ends the for loop)

ex:

   for i, v in pairs(game.Players:GetPlayers()) do
		if game.ReplicatedStorage.PlayerInGame:FindFirstChild(v.Name) then
			v.PlayerGui.Core.LoadingFrame.Visible = true
			v.PlayerGui.Core.Settings.Visible = false
			v.PlayerGui.Core.Menu.Visible = false
			for i, c in pairs(game.Workspace.MapFolder:GetChildren()) do
			  v.Character.Torso.CFrame = CFrame.new(c.Spawn.Tele.Position)
			end
			game.ReplicatedStorage.Bindables.LoadingStop:FireClient(v)
		    wait(1)
		else
			print("a")
		end
	end
    timer() -- Moved timer outside the loop

Let me know if this helped.

Can you give an example script on my script? Like mix it together? It really helped!

Edit: I want it to run for every player that has their name tag inside a folder.

Try this out:

 for i, v in pairs(game.Players:GetPlayers()) do
		if game.ReplicatedStorage.PlayerInGame:FindFirstChild(v.Name) then
			v.PlayerGui.Core.LoadingFrame.Visible = true
			v.PlayerGui.Core.Settings.Visible = false
			v.PlayerGui.Core.Menu.Visible = false
			for i, c in pairs(game.Workspace.MapFolder:GetChildren()) do
			  v.Character.Torso.CFrame = CFrame.new(c.Spawn.Tele.Position)
			end
			game.ReplicatedStorage.Bindables.LoadingStop:FireClient(v)
		    wait(1)
            spawn(timer) -- Just changed this. This will run the timer function for every player.
		else
			print("a")
		end
	end
1 Like

I don’t want everybody to get the timer. I just want everybody to get teleported.

Oh I see. Use the example I provided originally and that should work well.

That? Are you really sure if would work?

If your intent is to run the timer function only once, then yes, it will teleport the players and then run the timer right afterwards. :slight_smile:

There are a few things you could change to make the script a little cleaner, but I dont want to mess with it too much just incase your game is very particular.

I will test this soon, but thanks for the information! (Test if workable please).

1 Like

It didn’t work, sadly. It did the same thing.

There are so many things wrong with the code this thread hurts to read. I’m just going to post some general tips in response to the code here.

This code will teleport the player to every single part in MapFolder one after another. Idk what you’re trying to do.
Also, while pairs does work, ipairs is meant for arrays and is used identically.

game:GetService("Players") and game:GetService("ReplicatedStorage") are the correct way to get these services. The names of these services can be changed via code or in the explorer, and some other services don’t always exist and need to be created via GetService.

This line is likely the root of your problem. It waits one second before continuing to the next player in the list that is created on the first line. This list does not change, so players could leave or join during the loop causing an error or unexpected case. Waiting or yielding in pairs/ipairs loops can cause all sorts of problems, especially if the table changes during iteration.

First of all, changing Gui’s from the server is usually considered bad practice, but I’ll ignore that. What bothers me here is how these objects aren’t being localized when they are used multiple times. I see cases like this all the time. Here’s a less redundant way to write that:

local core = v.PlayerGui.Core
core.LoadingFrame.Visible = true
core.Settings.Visible = false
core.Menu.Visible = false

The design here is questionable, but what bothers me is the use of player.Name instead of player.UserId. While player.Name works, it could feasibly be changed during runtime without breaking any rules within the engine.

2 Likes

When you test the game, are you alone or are you testing it in two (with two dummies)? You should not use pairs but ipairs, because the function returns an array and ipairs is suitable to be used in arrays to yes, let’s just say that you should use the ipairs function. Then please add this to the second line:

print(i,v)

So you could see if your function works at all.

1 Like

please don’t use pcalls becouse they are Asynchronous (cannot yeld) u can use ypcall ( it can yeld)

There is always one thing in the map folder, and I am trying to teleport every player that has their name tag inside of a folder.

1 Like

This changed a few years ago, so pcall is identical to ypcall. Here’s a post from 2014:

But yes, please avoid pcall/ypcall in code that you as a developer actually write, they should be reserved for API calls that could error (like when using HttpService or DataStores); Not getting error messages from your own code is extremely confusing during development. If someone needs to safely wrap/spawn a thread without silencing errors you can find a few references to implementations in this post:

I never use a wait in a for loop when iterating through the players in a game. If you do this and have 20 players it will take 20 seconds before it completed the loop for the final player. Instead, end the loop, then wait, then do another for loop if needed. This way it will not conflict with other things in other scripts that will cause bugs depending on your player count.

@Eternalove_fan32
You don’t just use ipairs because it returns an array, it does not at all do that.
Keeping it short, using ipairs is what’s suggested due to how it works and it currently being the fastest generator, not to mention breaking the ‘loop’ on nil being encountered.

1 Like