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
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.
Roblox uses a datatype called "userdatas" for their instances. More information linked here. https://www.lua.org/pil/28.1.html
Use the name of v
and then compare the length of that to 1
with your condition.
if #v.Name > 1 then --// code end