Need help with GetPlayerFromServer

For some reason when I do “/bring all” and “/bring others” it doesn’t bring everybody only 1 player, is there anyway to fix this? I believe this script is the problem not the command itself. I can send the command as well if this isn’t the problem.

function GetPlayerFromServer(Player,String)
	local Found = {}
	local strl = String:lower()
	if strl == "all" then
		for i,v in pairs(game:GetService("Players"):GetPlayers()) do
			table.insert(Found,v)
		end
	elseif strl == "others" then
		for i,v in pairs(game:GetService("Players"):GetPlayers()) do
			if v.Name ~= Player.Name then
				table.insert(Found,v)
			end
		end   
	elseif strl == "me" then
		for i,v in pairs(game:GetService("Players"):GetPlayers()) do
			if v.Name == Player.Name then
				table.insert(Found,v)
			end
		end  
	else
		for i,v in pairs(game:GetService("Players"):GetPlayers()) do
			if v.Name:lower():sub(1, #String) == String:lower() then
				table.insert(Found,v)
			end
		end    
	end
	return Found    
end
1 Like

There doesn’t seem to be a problem with the code from what I can see, may we see the command’s code as well?

1 Like

sure

local prefix = "/"
local players = game:GetService("Players")

function GetPlayerFromServer(Player,String)
	local Found = {}
	local strl = String:lower()
	if strl == "all" then
		for i,v in pairs(game:GetService("Players"):GetPlayers()) do
			table.insert(Found,v)
		end
	elseif strl == "others" then
		for i,v in pairs(game:GetService("Players"):GetPlayers()) do
			if v.Name ~= Player.Name then
				table.insert(Found,v)
			end
		end   
	elseif strl == "me" then
		for i,v in pairs(game:GetService("Players"):GetPlayers()) do
			if v.Name == Player.Name then
				table.insert(Found,v)
			end
		end  
	else
		for i,v in pairs(game:GetService("Players"):GetPlayers()) do
			if v.Name:lower():sub(1, #String) == String:lower() then
				table.insert(Found,v)
			end
		end    
	end
	return Found    
end

players.PlayerAdded:Connect(function(plr) 
			plr.Chatted:Connect(function(msg)
				local split = string.split(msg," ")
				if string.lower(split[1]) == prefix.."bring" then
					local foundplayer = nil 
					for i,v in pairs(GetPlayerFromServer(plr,split[2])) do
						if v ~= nil then
							foundplayer=v
							break
						end
					end			
					if foundplayer ~= nil then
						if foundplayer then
					      foundplayer.Character.HumanoidRootPart.CFrame = plr.Character.HumanoidRootPart.CFrame
						end

					end
				end

			end)
		end)

That’s the whole code.

1 Like

Your issue seems to be that you’re only caring for the first non nil index in the result of GetPlayerFromServer judging by your foundplayer variable, hence why it only teleports 1 player

You have to put the CFrame changing code in the pairs loop and remove anything related to foundplayer

1 Like

Just a tip from a friend, you could format your code a little differently and better IMO too. Like this maybe:

function GetPlayerFromServer(Player,String)
	
	local Found = {}
	local strl = String:lower()
	
	if strl == "all" then
		
		local allPlayers = game:GetService("Players"):GetPlayers()
		return allPlayers
		
	elseif strl == "others" then
		
		local allPlayers = game:GetService("Players"):GetPlayers()
		table.remove(allPlayers, Player)
		
		return allPlayers
		
	elseif strl == "me" then
		
		return {Player}
		
	else
		
		for i,v in pairs(game:GetService("Players"):GetPlayers()) do
			if v.Name:lower():sub(1, #String) == String:lower() then
				table.insert(Found,v)
			end
		end    
	end
	
	return Found    
end
3 Likes

Thanks, I’ll try to fix that problem.

2 Likes

Change your player added event to this

players.PlayerAdded:Connect(function(plr) 
    plr.Chatted:Connect(function(msg)
        local split = string.split(msg," ")
		if string.lower(split[1]) == prefix.."bring" then
		    
		    for _,Player in ipairs(GetPlayerFromServer(plr,split[2])) do
		        Player.Character.HumanoidRootPart.CFrame = plr.Character.HumanoidRootPart.CFrame
		    end
		   
		end
	end)
end)
1 Like

table.remove takes a number for the second parameter as the “index to remove”. You would have to find the index number first before removing.

table.remove(allPlayers, table.find(allPlayers, Player))
1 Like