• Networked Key Presses?
    11 replies, posted
Is there any way for me to find if another player is pressing a key ('Use' for Example) client side?
Don't think so, i might be wrong though
[lua] local players = player.GetAll() for _, player in ipairs( players ) do if( player:KeyDown( IN_USE ) ) then Msg( "You pressed the forward key!\n" ) end end [/lua] ? Just tested it dosent seem to work on others :/
THat's because it is LOCAL, as in clientside
[QUOTE=nubcakez;16408048]THat's because it is LOCAL, as in clientside[/QUOTE] You mean the "local players" thing? Thats a local variable.. Only is set for the function its in...
Try KeyPressed instead, never had much luck with KeyDown.
Clientside you can only check your own keypressed. You can use the server to send keypresses to all clients with usermessages.
You would use data streams ;D
[QUOTE=cj1094;16413431]You would use data streams ;D[/QUOTE] What for? You would just send two extra usermessages and a bunch of other crap you wouldn't need.. Why waste bandwidth when you can send a pooled usermessage containing nothing that makes the clients report back?
He wants the client to speak to the server.
Just do this manually like [code] if input.IsKeyDown( KEY_E ) then --do stuff else --if not do other stuff end [/code]
init.lua [code] local announce = {} function announce.TellKeyPress(ply, whoPressed, key) local rp = ply if type(ply) == "table" then for _, v in pairs(ply) do rp:AddPlayer(v) end end umsg.Start("announce_keypress", rp) umsg.String(whoPressed:GetName()) umsg.String(key) umsg.End() end function announce.CatchKeyPress(ply, key) announce.TellKeyPress(player.GetByID(1), ply, key) end hook.Add("KeyPress", "CatchKeyPress", announce.CatchKeyPress)[/code] cl_init.lua [code]local function announceKeyPress(um) LocalPlayer():PrintMessage(HUD_PRINTTALK, um:ReadString() .. " has pressed " .. um:ReadString()) end usermessage.Hook("announce_keypress", announceKeyPress)[/code] Mind you, "key" is an integer, as it's an enum. :P So, you'll need to add in specific keys to understand which key is being pressed. So, just change the announce.CatchKeyPress() to: [code]function announce.CatchKeyPress(ply, key) if key != IN_USE then return end announce.TellKeyPress(player.GetByID(1), ply, "E") end hook.Add("KeyPress", "CatchKeyPress", announce.CatchKeyPress)[/code]
Sorry, you need to Log In to post a reply to this thread.