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

attempt to get length of local 'v' (a userdata value)?

Im trying to make admin commands that uses shortcuts to find player but its giving error whenever I try to execute command. Heres script:

function findPlayer(victim)
for i,v in pairs(game.Players:GetChildren()) do
if #victim < 3 then
print("Need more characters than 3 to find player.")
else
local lowname = v.Name:lower()
if lowname:sub(1,#victim) == victim then
if #v > 1 then
print("Enter more characters to find one player.")
else
return v.Name
end
else
print("Could not find existing player.")
end
end
end
end

2 answers

✓ Accepted answer

Whats wrong?

Well it's in the error. "Attempt to get length of a local v, a userdata value." You're trying to get the length # of variable "v" but its a userdata and has no __len metamethod set. http://lua-users.org/wiki/MetatableEvents

If we look through the code its right here where v is being defined. for i,v in pairs(game.Players:GetChildren()) do In this case the value of v is reference to a player.

The error occurs here if #v > 1 then for the reason that I described above.

Note

Roblox uses a datatype called "userdatas" for their instances. More information linked here. https://www.lua.org/pil/28.1.html

Solution

Use the name of v and then compare the length of that to 1 with your condition.

if #v.Name > 1 then
    --// code
end

6 comments

0

Im trying to get player count not name's long

0

#player:GetPlayers() will return the number of players ingame

0

I want to get v's count

0

He literally answered your question.

0

nope

0

bruh

Im trying to get player count not player's name length.

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