For loop of all game players is running only for one player

  1. What do you want to achieve? Keep it simple and clear!
    So i’m doing console commands, and to quickly find player by small string entered i use string.match for each player in players if they have that string in them
  2. What is the issue? Include screenshots / videos if possible!
    It only runs for one player. So like it checks me, and then stops
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub? I have no point why this happens

Code without some useless stuff:

["pex"] = {["Callback"] = function(executor, args)
		local PlayerManager = require(game:GetService("ServerScriptService").PlayerManager)
		for _,player in pairs(game:GetService("Players"):GetChildren()) do
			print(player.Name:lower(), table.remove(args, 1):lower())
			if string.match(player.Name:lower(), "^"..table.remove(args, 1):lower()) then
           ...
  	    end
	end
end,

Note: i provided code of one command, but this happens in all commands

is this in a local script or in a server script

Module script located in ServerScriptService under some other scripts
image

I tryed added continue if player is not match, but this doesn’t help

One day later, still can’t figure issue. I don’t know why for loop stops from running

Could you provide a bit more of the code? Is there a break or return somewhere after the if-statement?

Here is full code of Commands module, everywhere where for loop looks for players this issue happens:

local Fly = workspace.RemoteEvents.Console.Functions.Fly


local teleportService = game:GetService("TeleportService")


local permissionIndex = {
	["root"] = 5,
	["hadmin"] = 4,
	["admin"] = 3,
	["mod"] = 2,
	["user"] = 1
}

return {
	["rejoin"] = {["Callback"] = function(executor)
		teleportService:Teleport(game.PlaceId, executor)
		local Console = require(script.Parent)
		local ConsoleUI = executor.PlayerGui.DevConsoleGui
		Console:Output(ConsoleUI, "Rejoining...")
	end,
	["Permission"] = 1},
	["reload"] = {["Callback"] = function(executor)
		local Console = require(script.Parent)
		local ConsoleUI = executor.PlayerGui.DevConsoleGui
			Console:Update(ConsoleUI, false)
	end,
	["Permission"] = 1},
	["info"] = {["Callback"] = function(executor, args)
		local Players = game:GetService("Players"):GetPlayers()
		for _,player in pairs(game:GetService("Players"):GetChildren()) do
			if string.match(player.Name:lower(), "^"..table.remove(args, 1):lower()) then
			local target = player
			local Console = require(script.Parent)
			local ConsoleUI = executor.PlayerGui.DevConsoleGui
			Console:PlayerInformation(target, ConsoleUI)
			else
				local Console = require(script.Parent)
				local ConsoleUI = executor.PlayerGui.DevConsoleGui
				Console:Output(ConsoleUI, "This player doesn't exists!")

			end
		end
	end,
	["Permission"] = 2},
	["pex"] = {["Callback"] = function(executor, args)
		local PlayerManager = require(game:GetService("ServerScriptService").PlayerManager)
		for _,player in pairs(game:GetService("Players"):GetChildren()) do
			print(player.Name:lower(), table.remove(args, 1):lower())
			if string.match(player.Name:lower(), "^"..table.remove(args, 1):lower()) then
				
				local Console = require(script.Parent)
				local ConsoleUI = executor.PlayerGui.DevConsoleGui
				
				local target = player
				local permGroup = table.remove(args, 1)
				local executorGroup = PlayerManager.GetValue(executor, "PermissionGroup")
				local targetGroup = PlayerManager.GetValue(target, "PermissionGroup")
				local executorIndex = permissionIndex[executorGroup]
				local targetIndex = permissionIndex[targetGroup]
				if executorIndex > targetIndex then
					PlayerManager.SetValue(target, "PermissionGroup", permGroup:lower())
					Console:Output(ConsoleUI, "You successfully updated "..target.Name.." Permission Group to "..permGroup)
				else
					Console:Output(ConsoleUI, "You don't have enough permissions to update "..target.Name.." Permission Group!")
				end
			else
				local Console = require(script.Parent)
				local ConsoleUI = executor.PlayerGui.DevConsoleGui
				Console:Output(ConsoleUI, "This player doesn't exists!")
				continue
			end
		end
	end,
	["Permission"] = 4},
	["fly"] = {["Callback"] = function(executor)

		local Console = require(script.Parent)
		local ConsoleUI = executor.PlayerGui.DevConsoleGui
		
		local char = executor.Character
		local Configs = char.Configs
		local CanFly = Configs.CanFly
		local PlayerManager = require(game:GetService("ServerScriptService").PlayerManager)
		if CanFly.Value == false then
			Fly:FireClient(executor, "enable")
			CanFly.Value = true
			Console:Output(ConsoleUI, "Flying has successfully been enabled.")
		else
			Fly:FireClient(executor, "disable")
			CanFly.Value = false
			Console:Output(ConsoleUI, "Flying has successfully been disabled.")
		end
	end,
	["Permission"] = 3},
	["bring"] = {["Callback"] = function(executor, args)
		for _,player in pairs(game:GetService("Players"):GetChildren()) do
			if string.match(player.Name:lower(), "^"..table.remove(args, 1):lower()) then
			local target = player
				
			local Console = require(script.Parent)
			local ConsoleUI = executor.PlayerGui.DevConsoleGui
			
			local executorChar = executor.Character
			local targetChar = target.Character
			local executorRoot = executorChar.HumanoidRootPart
			local targetRoot = targetChar.HumanoidRootPart
			targetRoot.Position = executorRoot.Position
			
			Console:Output(ConsoleUI, "You successfully been teleported "..target.Name.." to your position")
			else
				local Console = require(script.Parent)
				local ConsoleUI = executor.PlayerGui.DevConsoleGui
				Console:Output(ConsoleUI, "This player doesn't exists!")
				continue
			end
		end
	end,
	["Permission"] = 2},
	["to"] = {["Callback"] = function(executor, args)
		for _,player in pairs(game:GetService("Players"):GetChildren()) do
			if string.match(player.Name:lower(), "^"..table.remove(args, 1):lower()) then
				
				local Console = require(script.Parent)
				local ConsoleUI = executor.PlayerGui.DevConsoleGui
				
				local target = player
				local executorChar = executor.Character
				local targetChar = target.Character
				local executorRoot = executorChar.HumanoidRootPart
				local targetRoot = targetChar.HumanoidRootPart
				executorRoot.Position = targetRoot.Position
				Console:Output(ConsoleUI, "You successfully been teleported to "..target.Name.." position")
			else
				local Console = require(script.Parent)
				local ConsoleUI = executor.PlayerGui.DevConsoleGui
				Console:Output(ConsoleUI, "This player doesn't exists!")
				continue
			end
		end
	end,
	["Permission"] = 2},
	["tp"] = {["Callback"] = function(executor, args)
		local goalX = table.remove(args, 1)
		local goalY = table.remove(args, 1)
		local goalZ = table.remove(args, 1)
		if game:GetService("Players"):FindFirstChild(executor.Name) then
			
			local Console = require(script.Parent)
			local ConsoleUI = executor.PlayerGui.DevConsoleGui
			
			local executorChar = executor.Character
			local executorRoot = executorChar.HumanoidRootPart
			executorRoot.Position = Vector3.new(goalX, goalY, goalZ)
			Console:Output(ConsoleUI, "You successfully been teleported to entered coordinates")
		end
	end,
	["Permission"] = 2},
	["kick"] = {["Callback"] = function(executor, args)
		local PlayerManager = require(game:GetService("ServerScriptService").PlayerManager)
		for _,player in pairs(game:GetService("Players"):GetChildren()) do
			if string.match(player.Name:lower(), "^"..table.remove(args, 1):lower()) then

				local Console = require(script.Parent)
				local ConsoleUI = executor.PlayerGui.DevConsoleGui

				local target = player
				local reason = table.remove(args, 1)
				if reason == nil or "" then
					reason = "You have been kicked by "..executor.Name
				end
				local executorGroup = PlayerManager.GetValue(executor, "PermissionGroup")
				local targetGroup = PlayerManager.GetValue(target, "PermissionGroup")
				local executorIndex = permissionIndex[executorGroup]
				local targetIndex = permissionIndex[targetGroup]
				if executorIndex > targetIndex then
					target:Kick(reason)
					Console:Output(ConsoleUI, "You successfully kicked "..target.Name.."\nPlease note if you kicked player without any reason, you will get warning(as of AA rule)")
				else
					Console:Output(ConsoleUI, "You don't have enough permissions to kick "..target.Name.."!")
				end
			else
				local Console = require(script.Parent)
				local ConsoleUI = executor.PlayerGui.DevConsoleGui
				Console:Output(ConsoleUI, "This player doesn't exists!")
				continue
			end
		end
	end,
	["Permission"] = 2},
	--[[["noflood"] = {["Callback"] = function(executor, args)
		local flood = game.Workspace.ServerStorage.EventStuff.Water
		flood:Destroy()
	end,
	["Permission"] = 2}--]]
}

Thanks! :slight_smile:

One thing I would recommend changing is not require() the Console inside each function, it should only have to be required once at the top of the script.

Also, this is a module that is required by the server? If you’re trying to manipulate a player’s ScreenGui, it won’t work doing so on the server.

Does it fully go through the first iteration of the for loop? I would recommend putting some print lines to better see if it gets stuck and where exactly it makes it to before getting stuck.

One thing I would recommend changing is not require() the Console inside each function, it should only have to be required once at the top of the script.

This would cause recursive requiring. Because Console requires Commands, and if Commands Require Console this will give infinite loop, so requiring in functions break this

Also, this is a module that is required by the server? If you’re trying to manipulate a player’s ScreenGui, it won’t work doing so on the server.

Yes this one is required by module which is required by server. Also why won’t it work? It works very fine updating text after a lot of tests, issue is what code looking for only one player and then stops.

Does it fully go through the first iteration of the for loop? I would recommend putting some print lines to better see if it gets stuck and where exactly it makes it to before getting stuck.

Absolutely yes. Because i get output at the end Console:Output(...).

Alright prints helped me figure this. It was

print(player.Name, table.remove(args, 1):lower())

what i’m doing table.remove a lot of times(this are command arguments) and it fairly get’s nil there. So it will be fixed by just adding keywords variable at top of for loop.

EDIT: Yeah that fixed issue. Thanks

No worries! :slight_smile: Glad I could help.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.