How do i make code run when something happens in a certain timeframe

im trying to make a system where the player has to press a key on their keyboard before the time runs out and if they do not press the key, it will run a certain function. and when they do press the key in the small time frame then a different function runs. i cant find any answers anywhere so anything is going to be very helpful

userinput service binded to a remote event binded to a bunch of checks

for instance

timedout = true
game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent").OnServerEvent:Connect(function(player)
    if timehasexpired then
        func1()
        timedout = false
    else
        func2()
        timedout = false
end)
delay("some amount of time", function()
    if timedout then
        func1()
    end
end)