Hey guys, can anyone share a function that gets player entity by name and also auto-completes it if there's only a fraction of the name?
[lua]
function Xeer_FindsAPlayerToday( ply, target )
for k,v in pairs( player.GetAll() ) do
if v:Nick() == target then
return v
elseif target == ply then
ply:ChatPrint("Hey, it's you!")
else
ply:ChatPrint("This is not a valid target")
end
end
return nil
end
[/lua]
Here's how DarkRP does it:
[lua]
function FindPlayer(info)
local pls = player.GetAll()
-- Find by Index Number (status in console)
for k, v in pairs(pls) do
if tonumber(info) == v:UserID() then
return v
end
end
-- Find by RP Name
for k, v in pairs(pls) do
if string.find(string.lower(v:GetNWString("rpname")), string.lower(tostring(info))) ~= nil then
return v
end
end
-- Find by Partial Nick
for k, v in pairs(pls) do
if string.find(string.lower(v:Name()), string.lower(tostring(info))) ~= nil then
return v
end
end
return nil
end
[/lua]
[editline]29th January 2011[/editline]
[QUOTE=thejjokerr;27728473]Why not put it in the request section if you're asking for a complete script to be made?[/QUOTE]
Because it isn't a complete script, just a few lines which can be used in conjunction with other code to form a script.
Everything above is shit.
[lua]function Ply(name)
name = string.lower(name);
for _,v in ipairs(player.GetHumans()) do
if(string.find(string.lower(v:Name()),name,1,true) != nil)
then return v;
end
end
end
[/lua]
Don't you waste those precious microseconds!
[lua]local t
function Ply( name )
name = name:lower()
t = player.GetHumans()
for i = 1, #t do
if ( t[i]:Nick():lower():find( name, 1, true ) ) then
return ply
end
end
end[/lua]
[QUOTE=Overv;27730296]Don't you waste those precious microseconds!
[lua]local t
function Ply( name )
name = name:lower()
t = player.GetHumans()
for i = 1, #t do
if ( t[i]:Nick():lower():find( name, 1, true ) ) then
return ply
end
end
end[/lua][/QUOTE]
My function is 2 times faster. :v:
...on an empty server.
[QUOTE=Overv;27730553]...on an empty server.[/QUOTE]
And on a server with 7 other people.
Turns out ipairs is faster than looping over the table manually. I learn something new every day.
[QUOTE=Overv;27730656]Turns out ipairs is faster than looping over the table manually. I learn something new every day.[/QUOTE]
ipairs is only faster for sequential tables with numeric keys.
-snip-
Wrong thread. Necro post 4 lyfe
Sorry, you need to Log In to post a reply to this thread.