• Passing an argument from clientside to serverside through RunConsoleCommand.
    13 replies, posted
I seem to have a little problem with passing an argument from clientside to serverside using "RunConsoleCommand". (Maybe I just don't get it.) What I'm trying to do is something like: When a player is inside a vehicle, and pressed the key IN_ATTACK. The player runs a console command and passes on the vehicle entity so my serverside script can emit a sound. Here is what I had in mind(simplified version), [B]Clientside:[/B] [lua] function VehicleClaxon() Player = LocalPlayer() if Player:InVehicle then vehicle = Player:GetVehicle() if Player:KeyPressed(IN_ATTACK) then Player:RunConsoleCommand("VehicleHorn", vehicle) end end end hook.Add("KeyPressed", "SoundThatClaxon", VehicleClaxon) [/lua] [B]Server side:[/B] [lua] function SoundHorn(vehicle) local Pitch = 100; local SoundLevel = 100; vehicle:EmitSound("insert sound here", Pitch, SoundLevel) end concommand.Add("VehicleHorn") [/lua] Am I doing something like totally wrong, or?
You can't pass entities through console commands, you can however pass strings. You should pass the EntIndex of the vehicle instead using [b][url=http://wiki.garrysmod.com/?title=Entity.EntIndex]Entity.EntIndex [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b]. Then on ther server you can use [b][url=http://wiki.garrysmod.com/?title=Ents.GetByIndex]Ents.GetByIndex [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] on ther server to get the vehicle back from the index. [editline]09:09PM[/editline] You also have your arguments wrong in the concommand.Add callback. [b][url=http://wiki.garrysmod.com/?title=Concommand.Add]Concommand.Add [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] [editline]09:12PM[/editline] -snip-
Yes, sorry about that I just quickly wrote it here in chat. Thanks for replying so fast, I'll try this out. Nice! :)
Actually in this case you should be using [b][url=wiki.garrysmod.com/?title=Gamemode.KeyPress]Gamemode.KeyPress [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] on the server since IN_ keys are already sent to the server.
Your code is kind of exploitable and got the ability for causing chaos... you probbably want the player to not be able to emit the sound from any entity, select the entity on the server instead
[QUOTE=Crazy Quebec;21797990]Actually in this case you should be using [b][url=wiki.garrysmod.com/?title=Gamemode.KeyPress]Gamemode.KeyPress [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] on the server since IN_ keys are already sent to the server.[/QUOTE] This.
[QUOTE=Crazy Quebec;21797990]Actually in this case you should be using [B][URL="http://wiki.garrysmod.com/?title=Gamemode.KeyPress"]Gamemode.KeyPress [IMG]http://wiki.garrysmod.com/favicon.ico[/IMG][/URL][/B] on the server since IN_ keys are already sent to the server.[/QUOTE] Didn't had much luck with that one. Thanks all, I have enough info!
[lua]hook.Add("KeyPress", "Claxon", function(ply, key) if ply:InVehicle() and key == IN_ATTACK then ply:GetVehicle():EmitSound("Sound Here") end end)[/lua] [editline]09:25PM[/editline] Simple.
It really doesn't want to work. [lua]hook.Add("KeyPress", "Claxon", function(ply, key) if ply:InVehicle() and key == IN_ATTACK then ply:GetVehicle():EmitSound("vu_horn_double.wav", 100, 100) end end [/lua] I have the sound locally and on server.
I missed a parenthesis in my code, I have edited it now.
Ooh, just blindly copied it.
Should look like this: [lua]hook.Add("KeyPress", "Claxon", function(ply, key) if ply:InVehicle() and key == IN_ATTACK then ply:GetVehicle():EmitSound("vu_horn_double.wav", 100, 100) end end)[/lua]
[QUOTE=MakeR;21798474]I missed a parenthesis in my code, I have edited it now.[/QUOTE] You put your new parenthesis in the wrong place.
[QUOTE=Crazy Quebec;21798526]You put your new parenthesis in the wrong place.[/QUOTE] Look again :)
Sorry, you need to Log In to post a reply to this thread.