Networked Key Presses?

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!
" )
end
end
[/lua]
?

Just tested it dosent seem to work on others :confused:

THat’s because it is LOCAL, as in clientside

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

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



if input.IsKeyDown( KEY_E ) then
   --do stuff
else
   --if not do other stuff
end


init.lua



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)

cl_init.lua


local function announceKeyPress(um)
    LocalPlayer():PrintMessage(HUD_PRINTTALK, um:ReadString() .. " has pressed " .. um:ReadString())
end
usermessage.Hook("announce_keypress", announceKeyPress)

Mind you, “key” is an integer, as it’s an enum. :stuck_out_tongue: So, you’ll need to add in specific keys to understand which key is being pressed.

So, just change the announce.CatchKeyPress() to:


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)