Passing an argument from clientside to serverside through RunConsoleCommand.

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),

Clientside:

[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]

Server side:

[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 **[Entity.EntIndex

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Entity.EntIndex)**. Then on ther server you can use **[Ents.GetByIndex

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Ents.GetByIndex)** 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. **[Concommand.Add

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Concommand.Add)**

[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! :slight_smile:

Actually in this case you should be using **[Gamemode.KeyPress

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=Gamemode.KeyPress)** 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

This.

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]

You put your new parenthesis in the wrong place.

Look again :slight_smile: