local Players = game:GetService("Players")
local function StartsWith(String, Start)
return String:sub(1, #Start) == Start
end
local function FindPlayer(Name)
for i, Player in pairs(Players:GetPlayers()) do
if StartsWith(Player.Name:lower(), Name:lower()) then
return Player
end
end
return nil
end
print(FindPlayer("don"))
I am trying to find Donzee_Boi in players with only typing don as seen above, but it prints nil! Help!
local Players = game:GetService("Players")
local function StartsWith(String, Start)
print(String:sub(1, #Start) == Start)
return String:sub(1, #Start) == Start
end
local function FindPlayer(Name)
for i, Player in pairs(Players:GetPlayers()) do
if StartsWith(Player.Name:lower(), Name:lower()) then
print(StartsWith(Player.Name:lower(), Name:lower()))
return Player
end
end
return nil
end
print(FindPlayer("don"))
I put my code as that and it only printed 1 thin which was nil, and the source was to print(FindPlayer("don"))
Is this ran from a server script? If so, is the function called first thing in the script? You may not be present in the game by the time the function is called, try yielding the script for a second or two before printing the function.
Along with what @GalaxyGourmet said, you could try doing return Player.Name inside of the FindPlayer function and I am also curios to know what your print(StartsWith(Player.Name:lower(), Name:lower())) yields or is that what is yielding nil?
Returning the name changes almost nothing. When you print an instance, it’s name shows up in the output. None of the functions seem to yield, and StartsWith() seems to work.
local UserText = script.Parent.username.Boxx
function SetUser(player)
print(player)
end
UserText:GetPropertyChangedSignal("Text"):Connect(function()
if string.len(UserText.Text) ~= 0 then
local found = false
local username = ""
for _,v in pairs(game.Players:GetPlayers()) do
if string.sub(v.Name,1,#UserText.Text) == UserText.Text then
found = true
username = v.Name
end
end
if found then
SetUser(game.Players:WaitForChild(username))
else
SetUser(nil)
end
else
SetUser(nil)
end
end)
Thats a new script someone gave me and it works! Just how do I make it lowercase, so if someones name is in capital letters, and I type it in lowercase, it still detects it.
local UserText = script.Parent.username.Boxx
function SetUser(player)
print(player)
end
UserText:GetPropertyChangedSignal("Text"):Connect(function()
if string.len(UserText.Text) ~= 0 then
local found = false
local username = ""
for _,v in pairs(game.Players:GetPlayers()) do
if string.sub(string.lower(v.Name),1,#UserText.Text) == string.lower(UserText.Text) then
found = true
username = v.Name
end
end
if found then
SetUser(game.Players:WaitForChild(username))
else
SetUser(nil)
end
else
SetUser(nil)
end
end)