Question
How to call a player in a shortened string?
Answer
We can iterate over all the players in the game and see if the front of their name matches the given string.
How do we get the front of a name?
Well we can use the :sub method which all string datatypes have. When called it will return the text between the positions you gave it.
Example: someName:sub(1, #givenStr)
It will get the 1st part of the string and everything following that has a position less than or equal to the length of #givenStr.
Another Example: local a = ("I like apples"):sub(1, 5)
This would result in a being set to the string "I lik" because the first 5 letters of the string are I lik
Fuller Code Example
This would return the player who's name matches the given string.
In this case I would be in the game and it would find my player instance given the string SinisterMem.
It will define the ingame players, iterate over them, check if the name's partial is equal to the partial name, then if so it will return said player.
local players = game:GetService("Player")
local function getPlayerByPartialName(partialName)
local partialName = partialName:lower()
local ingamePlayers = players:GetPlayers()
for i, player in next, ingamePlayers do
local name = player.Name:lower()
local partial = name:sub(1, #partialName)
if partial == partialName then
return player
end
end
end
local sinisterMemories = getPlayerByPartialName("SinisterMem")
2 comments
wow you ninja'd me by 3 entire minutes.
I use the term ninja'd too! Do we know each other? Like I use the term ninja'd on a daily basis.