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

What is wrong with this script?

I just cant understand what is wrong with it, there is an error, it is marked at --

function findPlayer(name)
    for _, player in pairs(game.Players:GetPlayers()) do
        if player.Name:lower() == name:lower() then
            return player
            return true end -- 'end' expected(to close 'if' at line 3 near 'return
        end
    end
end

1 answer

I don't think your able to do that. :P Do this instead;

function findPlayer(plr) --Our function
for i,v in pairs(game.Players:GetPlayers()) do --Gets all the players that are currently in game.Players
if v.Name:lower():find(plr:lower()) then --If the function matches plr to the Player's name then
return v --It'll return the player
end --The end to end the 'if' statement
end --The end to end the code for the 'for' loop
return nil --Will return nil
end --The end to end the function

Hope this helped!

3 comments

0

Ima test it out

0

return nil is superfluous. A function without a return will already evaluate to nil. If you did want to return a tuple (multiple values) you could return v, true, however, this is unnecessary since v will be truthy while nil will not be.

0

Oh, sorry, I didn't really know that. Guess I'm still a bit Out-of-date. Haha! :P

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