Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Does using a for loop and going through all players work in a local script?

So, I have a local script in which when a player's username is entered into a textBox and another button is clicked it will loop through all of the players in the game. However, it's not working and when testing it on studio (Not sure this is where another problem may lie) it says that the player doesn't exist. I was just wondering whether I might have to send the player's name through remoteEvent to the server and then for the server to do the search, but I wasn't entirely sure. However, this is the code that I have got so far.

Code:

textButton.MouseButton1Click:Connect(function()
	textEntered = enterName.Text
	for i,v in pairs(players:GetPlayers()) do
		if textEntered == v.Name then
-- There is more but this is the only part that I imagine being useful for my problem

This just got edited

5 comments

0

Are you trying to check if the username that was entered is a player that is in the game?

0

Yes.

0

That's why I was unsure if I would have to use RE's.

0

Since you're not changing anything on the server, this can be done in a local script. You should just be able to compare the strings of the text entered with the names of each player in the game.

0

That's what I did do, but when testing on studio there seems to be an error. I'll edit that bit into the code and if that doesn't work I'll test it on Roblox itself and not studio. It works when I click play normally, but when I use the option of testing for 2< players and the players names are 'Player1' and 'Player2' it doesn't work.

2 answers

local Players = game:GetService("Players")

textButton.MouseButton1Click:Connect(function()
   for _,Users in pairs(Players:GetPlayers()) do
	  print(Users.Name) -- works. even on client side / server side.
   end
end)

The player service is accessible to the server and the client meaning it can be accessed via the client side.

I can't see anything obviously wrong with your script, but I wrote an example script that you can use to compare with yours to hopefully help you find the issue. This is what I used in my example:

A ScreenGui in StarterGui named "PlayerCheck" with a TextBox and a TextButton both with the default name inside of it

A LocalScript inside of StarterPlayer > StarterPlayerScripts

LocalScript:

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local playerCheckGui = player.PlayerGui:WaitForChild("PlayerCheck")
local textBox = playerCheckGui.TextBox
local textButton = playerCheckGui.TextButton

textButton.MouseButton1Click:Connect(function()
	local check = textBox.Text
	local currentPlayers = Players:GetChildren()
	for i = 1, #currentPlayers do
		if currentPlayers[i].Name == check then
			print(check .. " is in the game")
			return
		end
	end
	print("No player in the server by that name")
end)

If you have any questions or problems with this answer, let me know. I'd be happy to clarify or fix it.

Hope this helps!

This discussion is preserved as it was. New answers, comments, and votes are closed.