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

Can anyone help with string manipulation?

Asked by 10 years ago

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

Log in to vote
3
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

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
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. ZirutoHellfire 10 — 10y
0
# works on both strings and tables to mean their length. It's much briefer, and I believe less expensive, so I prefer it. BlueTaslem 18071 — 10y
Ad
Log in to vote
0
Answered by 10 years ago

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
0
That's what I have? v is returned as nil always. ZirutoHellfire 10 — 10y
0
You talking about mine or his? xImmortalChaos 565 — 10y
0
Nvm I see what you did xD ZirutoHellfire 10 — 10y
0
Say if I matched it to my name and got it to use the kick function on me, would it work? ZirutoHellfire 10 — 10y
View all comments (3 more)
0
Please don't just post code, but an explanation as well. Perci1 4988 — 10y
0
Yes it would work, and i did explain my code..? xImmortalChaos 565 — 10y
0
Wow someone game me -1 Reputation for my work .-. xImmortalChaos 565 — 10y

Answer this question