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

Finding a player?

So, for a command, I've got it set to run a function to find the player:

	local plyr = getPlayer(message:sub(7))

And heres the function:

function getPlayer(str)
	local plyr = {} 
str = str:lower()
	local l = string:len(str)
							for i,v in pairs(game.Players:children()) do
if v:IsA("Player") then
	if v.Name:sub(1, l) == str then
		plyr = v
	end
	end
end
end

But I get the error: calling 'len' on bad self (string expected, got table)

1 answer

##Problem

What the script thinks you're doing is running a function on the keyword string. When you try calling a function in a table with ":" the script will assume you're calling the function with the table itself being the first argument. That's why you may see

tab = {
i = 'Hello';
p = function(self)
    print(self.i)
end }

tab:p()

Or method similar to this. The script should end up printing the i variable. What "string" is, is basically an array of functions that is a keyword for all scripts.


##Solution

You can go two ways.

  1. Actually call the StringOrStringVariableHere:len() function on the actual string itself.
  2. Or use string.len(StringOrStringVariableHere)

##Final Script

function getPlayer(str)
    local plyr = {} 
    str = str:lower()
    local l = string.len(str)
    for i,v in pairs(game.Players:children()) do
        if v:IsA("Player") then
            if v.Name:sub(1, l) == str then
                plyr = v
            end
        end
    end
end

######Hope this helped. If you have any questions feel free to comment below.

2 comments

0

That fixed the error I was having, but now it won't recognize it as a player as it's a table. So I can't run :GetRankInGroup() or, for killing, having it do plyr.Character

0

Why not on line 8 just return the v variable and for the kill script do something like getPlayers(stringhere).Character:BreakJoints(). I can not help you in many other parts of the script if I can not see them.

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