I need help with finding player in local test. I have the script that can find player only by part of its name. When I launch the local test with 2 players and run script with “Player2” it just returns nil. The script:
function getPlayer(plr)
if plr then
local lowerName = string.lower(plr)
for _, player in pairs(players:GetPlayers()) do
if lowerName == string.sub(string.lower(player.Name), 1, #lowerName) then
return player
end
break
end
end
end
Does it work with Player1? What does it return? Have you tried printing lowerName and the result of string.sub(string.lower(player.Name), 1, #lowerName)? Print debugging works 90% of the time.
break stops the loop since it’s outside the if statement, causing it to only check one player, simply remove the break and the function will work fine and return already stops the function so no need to worry about that.
function getPlayer(plr)
if plr then
local lowerName = string.lower(plr)
for _, player in pairs(players:GetPlayers()) do
if lowerName == string.sub(string.lower(player.Name), 1, #lowerName) then
return player
end
end
end
end