I am currently working on a volleyball game, and I need to see which player from team ‘Red’ was the first to press a certain key (for example F). I don’t know how to do this, and I can’t find any post about it explaining exactly what I want.
Is this during the whole game, or at a certain moment in a lobby as a timed comparison type of thing.
Please copy/paste the script you are working with here and remember to put three reverse quote marks (```) before and after the script so it formats properly here.
To determine which Team Red player was the first to press the specified key, you can add a timestamp to the message sent via the RemoteEvent. Then, on the server side, you can compare timestamps to identify the earliest key press.
-- Client script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("PlayerKeyPressed")
local UserInputService = game:GetService("UserInputService")
local TeamRedKey = "f"
-- Function to detect key press
UserInputService.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode[TeamRedKey] then
remoteEvent:FireServer(os.time()) -- Include timestamp
end
end)
-- Server script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("PlayerKeyPressed")
local earliestTimestamp = math.huge
local earliestPlayer = nil
remoteEvent.OnServerEvent:Connect(function(player, timestamp)
if timestamp < earliestTimestamp then
earliestTimestamp = timestamp
earliestPlayer = player
end
end)
-- After all key presses have been received
if earliestPlayer then
print(earliestPlayer.Name .. " from Team Red was the first to press the key.")
else
print("No player from Team Red pressed the key.")
end
You make a RemoteEvent and a script that logs the key presses
You fire the RemoteEvent when user pressed F and then the server script does whatever you need to do
Example:
Localscript
local uis = game:GetService("UserInputService")
uis.InputBegan:Connect(function(t,k)
if not k then
if t.KeyCode == Enum.KeyCode.F then
game:GetService("ReplicatedStorage").Detect:FireServer()
end
end
end)
Script:
local firstPlr = nil
game:GetService("ReplicatedStorage").Detect.OnServerEvent:Connect(function(plr)
if not firstPlr then
firstPlr = plr
end
end)