Well,
the title may explain the problem badly but well.
What I want to know is, how I am able to use like, if I got a custom command which just runs like
rust.RunServerCommand("airdrop.toplayer", playername)
how can I set that if I put anything behind my actual /airdrop command, I am using this as the Arg "playername" or how do I even so stuff like that /airdrop is the main command and if I add a "help" for example so Im writing /airdrop help, how am i able to create those?
Im very sorry about my well I think bad explanation because I actually don't know how to call those commands which got extra Args...
Hope you still understand and can help me :v
Laser
Solved Using console args and commands? (Lua)
Discussion in 'Rust Development' started by LaserHydra, Mar 24, 2015.
-
Wulf Community Admin
Code:rust.RunServerCommand("airdrop.toplayer \"player name\"")
-
for example /airdrop LaserHydra, it should give out airdrop.toplayer LaserHydra, but if I would put in Garry it should give out airdrop.toplayer Garry -
Wulf Community Admin
The argument you use depends on which hook or function you use it ib. Generally it would be player.displayName, but in some places it could be connection.username or others.
-
command.AddChatCommand("airdrop", self.Object, "cmdAirdrop")
and this as function:
Code:function PLUGIN:cmdAirdrop(player) if player.net.connection.authLevel == 0 then return end rust.BroadcastChat("Airdrop summoned by", player.displayName) rust.RunServerCommand("airdrop.player", "playername") end
so if I use the command like that:
/airdrop Garry
the function is basicly just like that: (well I don't mean that It should change the "playername" to "garry" but just work as It would be - well you should really kow what I mean )
The airdrop isnt supposed to be spawned onto the player who used the command. That would be player.displayName then if im right...
As used for the Chat Message
Code:function PLUGIN:cmdAirdrop(player) if player.net.connection.authLevel == 0 then return end rust.BroadcastChat("Airdrop summoned by", player.displayName) rust.RunServerCommand("airdrop.player", "Garry") end
-
You would need to read the args argument in the function.
cmdAirdrop(player, cmd, args)
args contains everything written after the actual command. You then use the name and search the baseplayer with it. -
so what do I add i
I need to write the function like this?:
Code:function PLUGIN:cmdAirdrop(player, cmd, args) if player.net.connection.authLevel == 0 then return end rust.BroadcastChat("Airdrop summoned by", player.displayName) rust.RunServerCommand("airdrop.toplayer", args)
or do I need to add something to the "args", like args.something :v
-Laser
[DOUBLEPOST=1427227771][/DOUBLEPOST]-> Well it doesnt seem right....
Code:[Oxide] 9:09 PM [Error] Failed to call hook 'cmdMassdrop' on plugin 'Airdrops' File: airdrops.lua Line: 31 invalid arguments to method call: at NLua.Lua.ThrowExceptionFromError (Int32 oldTop) [0x00000] in :0 at NLua.Lua.CallFunction (System.Object function, System.Object[] args, System.Type[] returnTypes) [0x00000] in :0 at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (object,object[],System.Exception&) at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in :0
Code:function PLUGIN:cmdMassdrop(player, cmd, args) if player.net.connection.authLevel < 1 then return end rust.BroadcastChat("AIRDROP", cmd.args, "Airdrops summoned by", player.displayName) rust.RunServerCommand("massdrop", cmd.args) end
Code:function PLUGIN:cmdMassdrop(player, cmd, args) if player.net.connection.authLevel < 1 then return end rust.BroadcastChat("AIRDROP", args, "Airdrops summoned by", player.displayName) rust.RunServerCommand("massdrop", args) end
I just tried looking into the code of a plugin with a smillilar function and came to the "authlevel" plugin.
After I looked at it my function looked like this:
Code:function PLUGIN:cmdAirdrop(player, cmd, args) if args.Length ~= 2 then local targetPlayer = global.BasePlayer.Find(args[0]) if player.net.connection.authLevel == 0 then return end rust.BroadcastChat("Airdrop summoned by", player.displayName) rust.RunServerCommand("airdrop", targetPlayer) else rust.RunServerCommand("event.run", "") end end
the console gives out:
Code:[Oxide] 10:08 PM [Error] Failed to call hook 'cmdAirdrop' on plugin 'Airdrops' (IndexOutOfRangeException: Array index is out of range.) [Oxide] 10:08 PM [Debug] at NLua.MetaFunctions.GetMethodInternal (LuaState luaState) [0x00000] in :0 at NLua.MetaFunctions.GetMethod (LuaState luaState) [0x00000] in :0 at (wrapper native-to-managed) NLua.MetaFunctions:GetMethod (KeraLua.LuaState) at (wrapper managed-to-native) KeraLua.NativeMethods:LuaNetPCall (intptr,int,int,int) at KeraLua.Lua.LuaNetPCall (IntPtr luaState, Int32 nArgs, Int32 nResults, Int32 errfunc) [0x00000] in :0 at NLua.LuaLib.LuaPCall (LuaState luaState, Int32 nArgs, Int32 nResults, Int32 errfunc) [0x00000] in :0 at NLua.Lua.CallFunction (System.Object function, System.Object[] args, System.Type[] returnTypes) [0x00000] in :0 at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (object,object[],System.Exception&) at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in :0
-
Wulf Community Admin
Do you have plugins installed that provide the "airdrop" and "event.run" commands?
-
-
Wulf Community Admin
Code:function PLUGIN:cmdAirdrop(player, cmd, args) if args.Length ~= 2 then local targetPlayer = global.BasePlayer.Find(args[0]) if player.net.connection.authLevel == 0 then return end rust.BroadcastChat("Airdrop summoned by " .. player.displayName) rust.RunServerCommand("airdrop " .. targetPlayer) else rust.RunServerCommand("event.run") end end
-
Thanks, how do I know when to use , and when .. ?
-
Wulf Community Admin
-
okay, so in this case its used to convert a variable to a string? ^_^
Because as i know the RunServerCommand and the Broadcast Chat work with multiple.. Like rust.BroadcastChat("TEST", "This is a testmessage") -
Wulf Community Admin
-
Thanks for that, but I still got problems ^^
Now if I run it it gives me this error:
Code:[Oxide] 6:42 PM [Error] Failed to call hook 'cmdAirdrop' on plugin 'Airdrops' File: airdrops.lua Line: 93 attempt to concatenate local 'targetPlayer' (a userdata value): at NLua.Lua.ThrowExceptionFromError (Int32 oldTop) [0x00000] in :0 at NLua.Lua.CallFunction (System.Object function, System.Object[] args, System.Type[] returnTypes) [0x00000] in :0 at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (object,object[],System.Exception&) at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in :0
Code:function PLUGIN:cmdAirdrop(player, cmd, args) if args.Length ~= 2 then local targetPlayer = global.BasePlayer.Find(args[0]) if player.net.connection.authLevel == 0 then return end rust.BroadcastChat("AIRDROP", "Airdrop for a specific player summoned by " .. player.displayName) rust.RunServerCommand("airdrop " .. targetPlayer) else rust.BroadcastChat("AIRDROP", "Airdrop summoned by " .. player.displayName) rust.RunServerCommand("event.run") end end
You know I got that
Code:else rust.RunServerCommand("event.run")
That doesnt get triggered if I type /airdrop
And gives out that old error again...
Code:[Oxide] 6:45 PM [Error] Failed to call hook 'cmdAirdrop' on plugin 'Airdrops' (IndexOutOfRangeException: Array index is out of range.) [Oxide] 6:45 PM [Debug] at NLua.MetaFunctions.GetMethodInternal (LuaState luaState) [0x00000] in :0 at NLua.MetaFunctions.GetMethod (LuaState luaState) [0x00000] in :0 at (wrapper native-to-managed) NLua.MetaFunctions:GetMethod (KeraLua.LuaState) at (wrapper managed-to-native) KeraLua.NativeMethods:LuaNetPCall (intptr,int,int,int) at KeraLua.Lua.LuaNetPCall (IntPtr luaState, Int32 nArgs, Int32 nResults, Int32 errfunc) [0x00000] in :0 at NLua.LuaLib.LuaPCall (LuaState luaState, Int32 nArgs, Int32 nResults, Int32 errfunc) [0x00000] in :0 at NLua.Lua.CallFunction (System.Object function, System.Object[] args, System.Type[] returnTypes) [0x00000] in :0 at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (object,object[],System.Exception&) at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in :0
Code:function PLUGIN:cmdMassdrop(player, cmd, args) if player.net.connection.authLevel < 1 then return end if args.Length ~= 2 then local dropAmount = string.lower(args[1]) if dropAmount < 2 then return end rust.BroadcastChat("AIRDROP" .. dropAmount, "Airdrops summoned by" .. player.displayName) rust.RunServerCommand("massdrop" .. dropAmount) else rust.SendChatMessage(player, "AIRDROP", "Syntax = /massdrop [AMOUNT]") end end
Code:[Oxide] 6:46 PM [Error] Failed to call hook 'cmdMassdrop' on plugin 'Airdrops' (IndexOutOfRangeException: Array index is out of range.) [Oxide] 6:46 PM [Debug] at NLua.MetaFunctions.GetMethodInternal (LuaState luaState) [0x00000] in :0 at NLua.MetaFunctions.GetMethod (LuaState luaState) [0x00000] in :0 at (wrapper native-to-managed) NLua.MetaFunctions:GetMethod (KeraLua.LuaState) at (wrapper managed-to-native) KeraLua.NativeMethods:LuaNetPCall (intptr,int,int,int) at KeraLua.Lua.LuaNetPCall (IntPtr luaState, Int32 nArgs, Int32 nResults, Int32 errfunc) [0x00000] in :0 at NLua.LuaLib.LuaPCall (LuaState luaState, Int32 nArgs, Int32 nResults, Int32 errfunc) [0x00000] in :0 at NLua.Lua.CallFunction (System.Object function, System.Object[] args, System.Type[] returnTypes) [0x00000] in :0 at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (object,object[],System.Exception&) at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in :0
-
You are trying to use a userdata as a string. Try
Code:function PLUGIN:cmdAirdrop(player, cmd, args) if args.Length ~= 2 then local targetPlayer = global.BasePlayer.Find(args[0]) if player.net.connection.authLevel == 0 then return end rust.BroadcastChat("AIRDROP", "Airdrop for a specific player summoned by " .. player.displayName) rust.RunServerCommand("airdrop " .. targetPlayer.displayName) else rust.BroadcastChat("AIRDROP", "Airdrop summoned by " .. player.displayName) rust.RunServerCommand("event.run") end end
-
Wulf Community Admin
-
Thanks Domestos!
Whats about the Massdrop error? Ive gone over it about 20 times now and didnt find anything. I don't know if im just to tired to see it or if eh I don't know what else :v
-Laser
[DOUBLEPOST=1427486554][/DOUBLEPOST]+ Also I noticed that whatever I put behind that "else" it wont work... It just says
Code:[Oxide] 9:02 PM [Error] Failed to call hook 'cmdAirdrop' on plugin 'Airdrops' (IndexOutOfRangeException: Array index is out of range.) [Oxide] 9:02 PM [Debug] at NLua.MetaFunctions.GetMethodInternal (LuaState luaState) [0x00000] in :0 at NLua.MetaFunctions.GetMethod (LuaState luaState) [0x00000] in :0 at (wrapper native-to-managed) NLua.MetaFunctions:GetMethod (KeraLua.LuaState) at (wrapper managed-to-native) KeraLua.NativeMethods:LuaNetPCall (intptr,int,int,int) at KeraLua.Lua.LuaNetPCall (IntPtr luaState, Int32 nArgs, Int32 nResults, Int32 errfunc) [0x00000] in :0 at NLua.LuaLib.LuaPCall (LuaState luaState, Int32 nArgs, Int32 nResults, Int32 errfunc) [0x00000] in :0 at NLua.Lua.CallFunction (System.Object function, System.Object[] args, System.Type[] returnTypes) [0x00000] in :0 at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (object,object[],System.Exception&) at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in :0
-
Code:if args.Length ~= 2 then local dropAmount = string.lower(args[1]) if dropAmount < 2 then return end rust.BroadcastChat("AIRDROP" .. dropAmount, "Airdrops summoned by" .. player.displayName) rust.RunServerCommand("massdrop" .. dropAmount) else
Secondly, args is an array of strings (string[ ]) and an array is zero-based. This means it starts counting at 0 and not at 1. So you would have to put args[0] in your dropAmount. Also keep in mind that it is a string and a number so you should cast it with the tonumber() function in lua so you would have something like this: local dropAmount = tonumber(args[0])
If you change these things then it should no longer throw the error. -
I really appreciate your help All of you.
-Laser
[DOUBLEPOST=1427580447,1427492640][/DOUBLEPOST]One more thing to this subject.
If I now want multibple args to be an output. For example for a Admin Messaging plugin...
So that I do like /admin Hello, I got a question!
and I get ADMIN REQUEST: Hello, I got a question!
because if I use it as before it just writes the first word. Do I need to put the Text in "" for that or does it also work without anyhow?