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

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

Asked by
zValerian 108
5 years ago

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

Log in to vote
0
Answered by
WoolHat 53
5 years ago

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))

Ad

Answer this question