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

Why does it give an error when I try to use string.sub to do something? (can't index nil value?!)

So I am trying to make it that when you say, "appat" and then a players name, it teleports you them. However, I keep getting this stupid error:

https://gyazo.com/2733f0c0e2aca93b2c6b59918cfdf3be

Here is the code:

game.Players.PlayerAdded:Connect(function(player)
    player.Chatted:Connect(function(msg)
        local lower = string.lower(msg) -- turns the letters inside the string to lowercase

        if string.match(lower,"appat ") then 
            local sub = string.sub(msg,6,100)

	print(sub.. "EXEISITIST")
dude = game.Players:FindFirstChild(sub).Name


local animation = game.Players:FindFirstChild(sub).Character.Humanoid:LoadAnimation(game.Players:FindFirstChild(sub).Backpack.flight)
        animation:Play()

thats just a snippet of it

does anyone know how to fix this or why it's happening?

1 answer

I believe the issue is that you are including the space (sixth character) when you chop your "lower" string on line 6. Because of that, you are seeking a player not named "zValerian", but "_zValerian".

An easy fix: Make your six into a seven (local sub = string.sub(msg,6,#lower))

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