Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Finding a player in game.Players?

Asked by
iFlusters 355 Moderation Voter
8 years ago

How can I find a player no matter the string? So,

player = "player1"
--or
player = "PLAYER1"

both would be able to find "Player1", I tried FindFirstChild() doesn't seem to work..

2 answers

Log in to vote
3
Answered by
Sublimus 992 Moderation Voter
8 years ago

There is more efficient ways to do this, but here it goes:

players = game.Players:getChildren() -- Pack players into a table
wantedPlayer = string.lower("SOMeNam3") -- Whatever you pass instead of this name will be converted to lowercase.
for _,v in pairs(players) do -- For every player (v) in players
    if string.lower(v.Name) == wantedPlayer then -- If lowercase version of player name matches lowercase version of wantedPlayer
        found = v -- Set the variable found equal to the player with the correct name
        break -- End loop to save memory
    end
end
-- Whatever you want to do with found player

string.lower()

0
Looks good. Nice answer. User#11440 120 — 8y
0
Thank you. Sublimus 992 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

Oh Hey buddy!

Nice to meet ya! So, you want find out a player no matter the name is written?

Well remember this before we can go onto... Let Player 1's name be PlayerofROBLOX Let Player 2's name be PlayerOFroblox

Now, in string manipulation, we have a pre-defined function called lower() What's the purpose? It gives you the string in a lowercase format, or less formal way xD

Like for example, If you apply the string.lower() function where string is the name of your player for instance, The function is going to return you the same name, but all the characters in it will be lowercase. In your case, it's gonna be a bit hard though.

For example, if you want to find the player and the function returns you the same string value for both players? (look up)

that means, Player1's name is gonna be just 'playerofroblox' and that applys to Player2's name too 'playerofroblox'

Hmmm, so possibly you want to make an admin list and then want to do stuff with only some people... For instance you want to make a script that will see if the player is on a list if players, and if he is, We do something to him (perhaps kick/ban him?)

Lets get coding

-- ServerScript placed in ServerScriptService

Allowed = {"Player1", "Player2"} -- Basically a list of people who are allowed to do something or not allowed LOL

function isAllowed(player) -- Define a function that checks if the passed parameter(player) is allowed to do something or not
    for k, v in next, Allowed do -- loop through our table to see if a player matching
        if v == player.Name then 
            return true -- Hey we got the player we want,
            break -- so lets break the loop so it doesn't lag us :9 
        end
    end
    return false -- We didn't get our player here return false pls!
end

--[[  
  Now basically the above function is really useful for admin only stuff or even for making restricted doors and stuff =D
]]

Well, that's all that I can help,(Im sleepy so don't mind if I explained it bad or even made a bad answer)

Anyways, Cya around!

0
What's with all the LOLing and XDing? iFlusters 355 — 7y

Answer this question