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

Can anyone help with string manipulation?

The following code always returns nil which is there error(there is more into the script but this is what I need to be fixed)

function SearchPlr(text)
for i, v in next,game.Players:GetPlayers() do
if string.find(v.Name,"^"..text) then
return v.Name:lower()
end
end
end

No matter what I do v:lower() or v.Name:lower() it always returns nil =/

2 answers

You should use a lowercase version of both the search text and the text to be searched in:

if v.Name:lower():find( "^" .. text:lower() ) then

Though, instead of using find and the caret, it might be wiser to just use substrings:

if v.Name:sub(1, #text):lower() == text:lower() then

2 comments

0

Wouldn't I use: if v.Name:sub(1, string.len(text)):lower() == text:lower() then -- string.len() because text isn't a table.

0

works on both strings and tables to mean their length. It's much briefer, and I believe less expensive, so I prefer it.

Sure, I can.

function SearchPlr(Text) --Your function.
for i,v in pairs(game.Players:GetPlayers()) do --Gets all the players in the game.
if v.Name:lower():find(Text:lower()) or v.Name:lower() == Text:lower() then --See's if the player's name matches the Text or has part of the Player name as the text.
return v.Name:lower() --Returns the player name lowered.
end
end
end

7 comments

0

That's what I have? v is returned as nil always.

0

You talking about mine or his?

0

Nvm I see what you did xD

0

Say if I matched it to my name and got it to use the kick function on me, would it work?

0

Please don't just post code, but an explanation as well.

0

Yes it would work, and i did explain my code..?

0

Wow someone game me -1 Reputation for my work .-.

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