Hi, I was wandering if it’s possible for a players client to detect other player’s input? For example player 1 client script does something when player 2 clicks or presses a key, ect. Note: I don’t want to use remote events / remote functions. Cheers.
Unfortunately, it is not possible to do this without RemoteEvents or RemoteFunctions because two clients are entirely separate from each other, and the server cannot read keypresses unless remotes are sent to the server. To do this you should set up a RemoteEvent in ReplicatedStorage, a LocalScript in PlayerGui or StarterPlayerScripts, and then a Server Script for capturing and sending events to other clients. Here’s an example:
-- Server
local RP = game.ReplicatedStorage
local Event = RP.RemoteEvent
Event.OnServerEvent:Connect(function(player, CallReason, ...)
local Args = {...}
if CallReason == "KeyPress" then
local Key = Args[1]
if Key and player then
Event:FireAllClients("KeyPress", player, Key)
end
end
end)
-- Client (For Everyone)
local RP = game.ReplicatedStorage
local Event = RP.RemoteEvent
local UserInput = game:GetService("UserInputService")
Event.OnClientEvent:Connect(function(CallReason, ...)
local Args = {...}
if CallReason == "KeyPress" then
local PlayerPressed = Args[1]
local Key = Args[2]
if PlayerPressed ~= game.Players.LocalPlayer then
-- Do whatever you want here with the player and the input object.
end
end
end)
UserInput.InputBegan:Connect(function(Input)
if Input.UserInputType == Enum.UserInputType.Keyboard then
Event:FireServer("KeyPress", Input.KeyCode.Name)
end
end)
2 Likes