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

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

Asked by
enes223 327 Moderation Voter
5 years ago

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

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

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
0
Im trying to get player count not name's long enes223 327 — 5y
0
`#player:GetPlayers()` will return the number of players ingame EpicMetatableMoment 1444 — 5y
0
I want to get v's count enes223 327 — 5y
0
He literally answered your question. User#24403 69 — 5y
View all comments (2 more)
0
nope enes223 327 — 5y
0
bruh EpicMetatableMoment 1444 — 5y
Ad
Log in to vote
0
Answered by
enes223 327 Moderation Voter
5 years ago

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

Answer this question