Your code formatted right looks like:
local stamina = 100
local user = game:GetService("UserInputService")
local Player = game:GetService("Players")
local player = game.Players.LocalPlayer
local Q = Enum.KeyCode.Q
local function IsQkeydown(Q)
return user:IsKeyDown(Q)
end
local function Input(input, input)
if not IsQkeydown(Q) then
else
if user:IsKeyDown(Q) and Q == Enum.KeyCode.Q then
stamina = stamina - 10
if stamina <= 99 then
wait(5)
while stamina <= 99 do
stamina = stamina + 10
if stamina == 100 then
end
end
end
end
end
end
user.InputBegan:Connect(Input)
The main problem is how you use IsKeyDown. Ignoring other issues, the code calls to check if the Q button is held down and does it only once. What happens when they let go of it? The while loop doesn't even know when to stop (You code doesn't get there anyways).
Instead of doing that, you could use InputBegan and InputEnded in tandem to call the Input() function:
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
function onInput(inputObject, gameProccessed)
if inputObject.UserInputState == Enum.UserInputState.Begin then
elseif inputObject.UserInputState == Enum.UserInputState.End then
end
end
UserInputService.InputBegan:Connect(onInput)
UserInputService.InputEnded:Connect(onInput)
Now we have the ability to see whenever a key is released. Right now this detects any key being clicked, like the mouse keys. To stop this we include a check for InputObject's KeyCode.
We also need a switch which functions as the key being held (A bool type). We make it true when the Q is press and false when it's released.
local key = Enum.KeyCode.Q
local keyHeld = false
function onInput(inputObject, gameProccessed)
if inputObject.UserInputState == Enum.UserInputState.Begin then
if inputObject.KeyCode == key and not keyHeld then
keyHeld = true
end
elseif inputObject.UserInputState == Enum.UserInputState.End then
if inputObject.KeyCode == key and keyHeld then
keyHeld = false
end
end
end
This is a simple way to make a on Key held mechanic. You can use :IsKeyDown but this way always stays within the onInput function. I believe the IsKeyDown method is put into some loop.
To test the final product I will use a while-loop to detect when keyHeld is ever true. If it is then it'll print something.
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local key = Enum.KeyCode.Q
local keyHeld = false
function onInput(inputObject, gameProccessed)
if inputObject.UserInputState == Enum.UserInputState.Begin then
if inputObject.KeyCode == key and not keyHeld then
keyHeld = true
end
elseif inputObject.UserInputState == Enum.UserInputState.End then
if inputObject.KeyCode == key and keyHeld then
keyHeld = false
end
end
end
UserInputService.InputBegan:Connect(onInput)
UserInputService.InputEnded:Connect(onInput)
while true do
if keyHeld then
print("Running!")
end
wait()
end
The last thing for you to do is to handle the stamina decreasing when it's held. Let me know if you've understood what I've given or if I missed something.
1 comment
Remember to ACCEPT my Answer below! (;