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

String.sub doesn't work on lowercase?

Asked by 6 years ago

Hi guys!

So I wrote this small script that basically adds a GUI ontop of someone's head when an admin chats a certain command. In this case that would be "wanted/(USERNAME)".

The script works perfectly fine, when typed, the user gets a GUI ontop of their head. My only problem is; the username MUST be typed with correct capitals. Example;

user = Player1

"wanted/Player1" <-- Works

"wanted/player1" <-- Does not work!

How can I make sure the script works even with lowercases, whilst still using the string.sub?

My function:

if string.sub(msg, 1,7) == "wanted/" and game.Players:FindFirstChild(string.sub(msg, 8)) then                       
local guiclone = gui:Clone()
guiclone.Parent = game.Players:FindFirstChild(string.sub(msg,8)).Character.Head
guiclone.Enabled = true
print(string.sub(msg, 8).." is wanted!")
end
0
You'd have to create a separate function that retrieves the player. TheeDeathCaster 2368 — 6y

1 answer

Log in to vote
0
Answered by
xAtom_ik 574 Moderation Voter
6 years ago

I would write a separate function which would loop through the players, putting their names in lowercase and putting the sub in lowercase.

Like this:

function GetPlayer(input)
    local inputLower = string.lower(input)
    for i,v in pairs(game.Players:GetChildren()) do
        local lowerName = string.lower(v.Name)
        if lowerName == inputLower then
            return v
        end
    end
    return nil
end

To use it in your code, you would use this:

if string.sub(msg, 1,7) == "wanted/" then
    local player = GetPlayer(string.sub(msg, 8))
    if player then                 
        local guiclone = gui:Clone()
        guiclone.Parent = player.Character.Head
        guiclone.Enabled = true
        print(player.Name .." is wanted!")
    end
end
0
STOP STEALINGZ MAI ANZERZ! TheeDeathCaster 2368 — 6y
0
Worked like a charm, thanks! User#20989 0 — 6y
0
Thee instead of commenting just answer then. xAtom_ik 574 — 6y
Ad

Answer this question