stamina = 100 local user = game:GetService("UserInputService") local Q = Enum.KeyCode.Q local function IsQkeydown(Q) print(stamina) Q = Enum.KeyCode.Q return user:IsKeyDown(Q) end local function Input (Input) if not IsQkeydown(Q) then else 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 user.InputBegan:Connect(Input)
When I right click the stamina value goes down how can i fix this?
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 --nothing happens here 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 --nothing happens here 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:
--Give proper names to services. local UserInputService = game:GetService("UserInputService") local Players = game:GetService("Players") function onInput(inputObject, gameProccessed) if inputObject.UserInputState == Enum.UserInputState.Begin then --When a key is pressed elseif inputObject.UserInputState == Enum.UserInputState.End then --When a key is released 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.
--Give proper names to services. 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) --Example test you shouldn't keep: 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.
It's a tool used to handle user generated events.
Stamina is a mechanic which destroys the player character, it usually involves the character, and he/she tries to refill their stamina constantly to remain in league with competitive style gameplay.
Want More - Just Ask!