I would firstly like to thank you for being so patient, as most learning developers tend to rush and miss out on important stuff. Scripting takes great patience, especially in Roblox since the API can seem quite menacing. I'll be showing you step-by-step how to solve this seemingly simple problem. The answer is simple, but requires explaining.
#Services#
Services in Roblox are top-class objects that can be obtained using the GetService method of game (game:GetService()). They make things much easier on the developer by coming packed with an array of useful functions and events.
#UserInputService#
Usually shortened to UIS, this service comes in handy when detecting user input (the name says it all). This can be obtained by doing game:GetService("UserInputService"), as with any other service. Please note that this service can only be used in LocalScripts.
#InputBegan#
UIS.InputBegan is like any other event, as it can be Connected to a function. It fires whenever a user input is detected (typically a key press).
This is a lot to take in, so let's see where we are so far.
local uis = game:GetService("UserInputService")
uis.InputBegan:Connect(function(key)
print(key.KeyCode)
end)
#Loops#
Loops are essential in Lua: Loops allow portions of code to be repeated until a condition is met (or indefinitely, commonly referred to as an infinite loop).
We can use loops to constantly check whether the key is pressed or not, using variable isPressed:
local uis = game:GetService("UserInputService")
local isPressed = false
uis.InputBegan:Connect(function(key)
if key.KeyCode == Enum.KeyCode.F then
isPressed = true
end
end)
uis.InputEnded:Connect(function(key)
if key.KeyCode == Enum.KeyCode.F then
isPressed = true
end
end)
while true do
print(isPressed)
wait()
end
InputEnded fires when a key is becomes unpressed.
We're very close; try it out. If you hold F, it will print true, elsewise it will print false.
What we can do now is use a holdTime variable to see how long we've been holding the key. This determines whether we've held the key long enough once we release the key.
local uis = game:GetService("UserInputService")
local isPressed = false
local holdTime = 0
uis.InputBegan:Connect(function(key)
if key.KeyCode == Enum.KeyCode.F then
isPressed = true
end
end)
uis.InputEnded:Connect(function(key)
if key.KeyCode == Enum.KeyCode.F then
isPressed = true
end
end)
while true do
while isPressed or holdTime < 2 do
wait(0.1)
holdTime = holdTime + 0.1
end
print(holdTime)
holdTime = 0
wait()
end
Our input mechanic is almost complete! Now, we just need to send a message to the Server saying we need to reset. We can do so using RemoteEvents. Don't worry, we won't be doing anything too complicated:
Insert a RemoteEvent into ReplicatedStorage, and a (regular) Script into ServerScriptService:
local uis = game:GetService("UserInputService")
local isPressed = false
local holdTime = 0
uis.InputBegan:Connect(function(key)
if key.KeyCode == Enum.KeyCode.F then
isPressed = true
end
end)
uis.InputEnded:Connect(function(key)
if key.KeyCode == Enum.KeyCode.F then
isPressed = true
end
end)
while true do
while isPressed or holdTime < 2 do
wait(0.1)
holdTime = holdTime + 0.1
end
if holdTime >= 2 then
game.ReplicatedStorage.RemoteEvent:FireServer()
end
repeat wait() until isPressed == false
holdTime = 0
wait()
end
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(plr)
if plr.Character then
plr.Character.Humanoid.Health = 0
end
end)
Hope I helped! Remember: the Wiki is your goto if you don't know how something works.
3 comments
Player:GetResetButton()?
instead of GetResetButton(), do :GetMouse(), even though this method of input is deprecated, i highly recommend you use UseInputService InputBegan
KeyDown sucks and as ruben said it's deprecated, and here's the link https://www.robloxdev.com/api-reference/class/UserInputService This is a much better way of handling user input as not only can you do computers but also console and mobile players. It'll help make games cross platform compatible.