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

If statement .. syntax error?

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

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

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