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

If statement .. syntax error?

Asked by
ImageLabel 1541 Moderation Voter
9 years ago

Output [string "stdin"]:8: 'then' expected near 'return'attempt to call a nil value

I don't understand why my easy script is not working. In other languages I program in, there's no such thing as "then".

local getPlayer = function(playerName)
    local players = game.Players:GetPlayers()

    for index = 1, #players do
        local name = players[index].Name:upper()

        if name:find(playerName:upper()) --removed then
            return players[index] -- returns player at `index`
        end
    end
end

1 answer

Log in to vote
0
Answered by
ImageLabel 1541 Moderation Voter
9 years ago

if condition then execute end

then indicates the end of your condition, and everything after indicates what would run after the assessment/evaluation.

local getPlayer = function(playerName)
    local players = game.Players:GetPlayers()

    for index = 1, #players do
        local name = players[index].Name:upper()

        if name:find(playerName:upper()) then -- added then
            return players[index] -- returns player at `index`
        end
    end
end
Ad

Answer this question