• Command only for serverside
    14 replies, posted
im wondering how i would make a command that players cannot execute, or maybe not even a command, just something players cannot execute [editline]1st December 2015[/editline] this needs to be executed on clientside to run a function serverside, i tried using IsPlayer() but that just returned true when i ran from console or using RunConsoleCommand
[URL="http://wiki.garrysmod.com/page/Player/IsSuperAdmin"]IsSuperAdmin()[/URL] [URL="http://wiki.garrysmod.com/page/Player/IsAdmin"]IsAdmin()[/URL] [URL="http://wiki.garrysmod.com/page/Player/GetUserGroup"]GetUserGroup()[/URL] That will prevent normal players from using the command. I can't tell if you are trying to keep normal players form using it or if you want only the server console to run it. If the second is the case you would use[URL="http://wiki.garrysmod.com/page/Entity/IsValid"] IsValid()[/URL] to check if it is the console like so. [CODE]if (ply:IsValid() == false) then return true end[/CODE]
Don't use :IsValid(). :IsValid() isn't a method for nil (but is for NULL, but it is safer when dealing with nil). Use IsValid(ply) But yeah, as said above. Just check if the player is NULL (!IsValid(ply)) and you'll know it's the console or a Lua script. Use the usergroup functions to detect the player's usergroup.
Why are you guys helping someone who leaks scripts?
So I am reading this and I think that IsValid(ply) is not the best way to do it. In this case you are dealing with the NULL entity. Use:[CODE]if ply == NULL then --Your code here end[/CODE] This prevents people that become invalid from being able to run the command. (Even if it is unlikely that they can.) But remember there is a small window of time where players are not valid when they join. (Maybe a lag spike can do the same?) Here is a benchmark incase you wondered:[CODE]if IsValid( ply ) then end --0.15724096239137 Seconds if ply == NULL then end --0.00072239288419951 Seconds [/CODE] Code was run 1000000 times and tested with a SysTime() call.
You know that nil and NULL aren't the same, right? lua_run print(nil == NULL) > print(nil == NULL)... false Are you calling IsValid 1,000,000 times a second in your code?
[QUOTE=Banana Lord.;49225068]You know that nil and NULL aren't the same, right? lua_run print(nil == NULL) > print(nil == NULL)... false Are you calling IsValid 1,000,000 times a second in your code?[/QUOTE] I am testing an entity not nil. Also I did a benchmark to test if it is even worth using it. You can say I am wrong but test it yourself. [CODE]concommand.Add( "testcon", function( ply ) print( ply == NULL ) end ) prints true[/CODE] I am completely right. So why risk potential problems when you can just do ply == NULL? On another note. I notice how you people respond negatively to things that are different from what they usually see. I suggest something and people just say NOPE without trying. If someone offers help take it from them and don't tell them they are wrong when they are not.
[QUOTE=darkjacky;49225281]I am testing an entity not nil. Also I did a benchmark to test if it is even worth using it. You can say I am wrong but test it yourself. [CODE]concommand.Add( "testcon", function( ply ) print( ply == NULL ) end ) prints true[/CODE] I am completely right. So why risk potential problems when you can just do ply == NULL? On another note. I notice how you people respond negatively to things that are different from what they usually see. I suggest something and people just say NOPE without trying. If someone offers help take it from them and don't tell them they are wrong when they are not.[/QUOTE] But the argument sent in can be nil. There's no guarantee for it to be an entity.
[QUOTE=code_gs;49225941]But the argument sent in can be nil. There's no guarantee for it to be an entity.[/QUOTE] Does that not prove my point? When you run it from the console it will be NULL. [CODE]game.ConsoleCommand("testcon\n") prints true RunConsoleCommand("testcon") prints true[/CODE] The only exception is:[CODE]concommand.Run( nil, "testcon" ) prints false[/CODE] But that one states: This is an internal function or feature. This means you will be able to call or use it, but you really shouldn't. Ok so, the OP is saying he wants to run a function from CLIENT to SERVER. "this needs to be executed on clientside to run a function serverside"(Clientside Concommand + net Library?) the console command executed on the client? In that case certainly IsValid(ply) is Correct. However he is also saying "just something players cannot execute" and that is something you can not do without checking for a steamid or anything like that. I guess he wants to be able to call it himself only. [CODE]if ( IsValid( ply ) and ply:SteamID() == "" ) or ply == NULL then -- in case of running it on the server if IsValid( ply ) and ply:SteamID() == "" then --in case of running it on the client[/CODE] Don't forget to check on the server if the request is correct.
The OP hasn't made it clear what exactly he wants this command to do. His post is hard to understand and it either seems like he wants regular players to not be able to run the command or he wants only the console to be able to use the command. It would be nice if he could reply here and clear up the details. At this rate he has gotten enough replies that he should have said something by now.
[QUOTE=darkjacky;49226672][CODE]cloudflare wont let me quote this[/CODE] [/QUOTE] That's going to error if the player isn't valid, since the entire statement is ran, and if ply is nil/NULL/whatever then you're calling functions on it that don't exist. You also don't need the ply:SteamID() == "" or the ply == NULL part, IsValid does that for you. Another thing: creating concommands on the server means they also exist on the client, and you can run them from there.
[QUOTE=meharryp;49227691]That's going to error if the player isn't valid, since the entire statement is ran, and if ply is nil/NULL/whatever then you're calling functions on it that don't exist. You also don't need the ply:SteamID() == "" or the ply == NULL part, IsValid does that for you. Another thing: creating concommands on the server means they also exist on the client, and you can run them from there.[/QUOTE] [CODE]if ( IsValid( ply ) and ply:SteamID() == "" ) or ply == NULL then[/CODE] will not error since it will check if the player is valid and if it is it will check the SteamID and if it is not it will check if it is the console. Lua runs from left to right if the first returns false it wont check the second unless there is an or statement. In this case it will check if the player is valid if it is it will check the steamid. If it is not it will check if it is NULL. nil will not be allowed.
[QUOTE=darkjacky;49227839][CODE]if ( IsValid( ply ) and ply:SteamID() == "" ) or ply == NULL then[/CODE] will not error since it will check if the player is valid and if it is it will check the SteamID and if it is not it will check if it is the console. Lua runs from left to right if the first returns false it wont check the second unless there is an or statement. In this case it will check if the player is valid if it is it will check the steamid. If it is not it will check if it is NULL. nil will not be allowed.[/QUOTE] Still, there's not need for the ply:SteamID() and ply == NULL part, IsValid does that for you.
Why do you even need a concommand to not be usable? Can't you just do it internally without setting/creating any commands?
[QUOTE=MPan1;49233854]Why do you even need a concommand to not be usable? Can't you just do it internally without setting/creating any commands?[/QUOTE] probably some useless addon that can only be activated through server console.
Sorry, you need to Log In to post a reply to this thread.