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

Finding a player?

Asked by
DevNetx 250 Moderation Voter
8 years ago

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

Log in to vote
0
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
8 years ago

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.
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 DevNetx 250 — 8y
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. M39a9am3R 3210 — 8y
Ad

Answer this question