I am having issues with the rust.FindPlayer function. This is the code I am using at the moment:
And the error i am recieving is:Code:function PLUGIN:cmdAllowPlayer(player, cmd, args) local target = rust:FindPlayer(args[0]) print(target.displayName) end
The only other relevant code to this is:Code:[Oxide] 6:06 PM [Error] Failed to call hook 'cmdAllowPlayer' on plugin 'CapaPlugin v0.0.1' File: CapaPlugin.lua Line: 37 attempt to call field 'FindPlayer' (a nil value): at NLua.Lua.ThrowExceptionFromError (Int32 oldTop) [0x00000] in <filename unknown>:0 at NLua.Lua.CallFunction (System.Object function, System.Object[] args, System.Type[] returnTypes) [0x00000] in <filename unknown>:0 at NLua.Lua.CallFunction (System.Object function, System.Object[] args) [0x00000] in <filename unknown>:0 at NLua.LuaFunction.Call (System.Object[] args) [0x00000] in <filename unknown>:0 at Oxide.Ext.Lua.Plugins.LuaPlugin.OnCallHook (System.String hookname, System.Object[] args) [0x00000] in <filename unknown>:0 at Oxide.Core.Plugins.Plugin.CallHook (System.String hookname, System.Object[] args) [0x00000] in <filename unknown>:0
I call it by typing "/allow Capadillo" in chat.Code:function PLUGIN:Init() command.AddChatCommand('allow', self.Object, "cmdAllowPlayer") end
I've both copied this code from another plugin and reviewed the Oxide.Game.Rust.dll file where this function is kept and can't find any clues as to why it's giving me the error as so far as I can tell, FindPlayer does exist.
FindPlayer Issue
Discussion in 'Rust Development' started by Capadillo, Feb 26, 2016.
-
Try this
Code:function FindPlayer(searchedName, player) local players = global.BasePlayer.activePlayerList:GetEnumerator() local matchingPlayers = {} while players:MoveNext() do if string.match(string.lower(players.Current), string.lower(searchedName)) then table.insert(matchingPlayers, players.Current) end end if matchingPlayers.Length == 1 then return matchingPlayers[0] elseif matchingPlayers.Length > 1 then rust.SendChatMessage(player, "Multiple matching players have been found:") for _, name in pairs(matchingPlayers) do rust.SendChatMessage(player, name) end return nil elseif matchingPlayers == 0 then rust.SendChatMessage(player, "No matching player has been found") return nil end end -
Thanks Ankawi! I had to modify it a little to the following otherwise it wouldn't work for me:
I was really hoping to be able to use a built-in library function, but this is just as good for the moment. I won't mark this as solved just yet though, as this is only a work around. Either way, really great response. ThanksCode:function PLUGIN:FindPlayer(searchedName, player) local players = global.BasePlayer.activePlayerList:GetEnumerator() local matchingPlayers = {} while players:MoveNext() do if string.match(string.lower(players.Current.displayName), string.lower(searchedName)) then table.insert(matchingPlayers, players.Current) end end if #matchingPlayers == 1 then return matchingPlayers[1] elseif #matchingPlayers > 1 then rust.SendChatMessage(player, "Multiple matching players have been found:") for _, name in pairs(matchingPlayers) do rust.SendChatMessage(player, name.displayName) end elseif #matchingPlayers == 0 then rust.SendChatMessage(player, "No matching player has been found.") end return nil end
EDIT: Rewrote it to be a little more like the built in functionality...
Code:local function FindPlayer(nameOrID, player) local activePlayer = global.BasePlayer.activePlayerList:GetEnumerator() local partialMatch = {} while activePlayer:MoveNext() do if activePlayer.Current.UserIDString == nameOrID or string.lower(activePlayer.Current.displayName) == string.lower(nameOrID) then return activePlayer.Current elseif string.match(string.lower(activePlayer.Current.displayName), string.lower(nameOrID)) then table.insert(partialMatch, activePlayer.Current) end end if #partialMatch == 1 then return partialMatch[1] elseif #partialMatch >= 2 then rust.SendChatMessage(player, "Multiple matching players have been found:") for key, partial in pairs(partialMatch) do rust.SendChatMessage(player, partial.displayName) end else rust.SendChatMessage(player, "No matching player has been found.") end return false endLast edited by a moderator: Feb 26, 2016 -
Its .FindPlayer() not :FindPlayer()
-
Doesn't make a difference. rust:FindPlayer() is just a shortcut for rust.FindPlayer(rust). If the error was saying something along the lines of "invalid parameter" or "string expected, got userdata" then I would fully agree with you.
At least it is in Lua. In either case, I tried both and received the same error.Last edited by a moderator: Feb 26, 2016
