I honestly am clueless, I've tried something by the likes of this:
function decreaseStamina()
while running == true do
Stamina = Stamina - 1
print(Stamina)
wait(.01)
end
end
but upon key release the loop would keep going.
I honestly am clueless, I've tried something by the likes of this:
function decreaseStamina()
while running == true do
Stamina = Stamina - 1
print(Stamina)
wait(.01)
end
end
but upon key release the loop would keep going.
I suggest UserInputService (LocalScript):
game:GetService("UserInputService").InputBegan:Connect(function(input)
if input.KeyCode = Enum.KeyCode.LeftShift then -- Replace LeftShift with any KeyCode
Stamina = Stamina - 1
print(Stamina)
wait(0.01)
end
end)
Edit: Wrote "Conenct" instead of "Connect"
NOTE: set this local script to startergui, starterpack, or starterscripts
-- Put in StarterCharacterScript as a LocalScript
local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
local input = game:GetService('UserInputService')
local reloading = false
local max_walkspeed = 32
local min_walkspeed = 16
local max_stamina = 30
local running = Instance.new("BoolValue") -- So we can use the Changed event
local stamina = Instance.new("NumberValue")
stamina.Value = max_stamina
local function change_walkspeed(ws)
humanoid.WalkSpeed = ws
end
local last_stamina = stamina.Value
stamina.Changed:connect(function()
print( (stamina.Value < last_stamina and 'Stamina Depleting: ' or 'Stamina Rising: ') ..stamina.Value )
last_stamina = stamina.Value
end)
running.Changed:connect(function()
if running.Value then
change_walkspeed(max_walkspeed)
else
change_walkspeed(min_walkspeed)
end
end)
input.InputBegan:connect(function(inputType)
if inputType.KeyCode == Enum.KeyCode.LeftShift and not reloading then
running.Value = true
while running.Value and stamina.Value > 0 do
stamina.Value = stamina.Value - 1
wait()
end
-- while loop breaks and stops us from running
running.Value = false
if stamina.Value <= 0 then
reloading = true
end
end
end)
input.InputEnded:connect(function(inputType)
if inputType.KeyCode == Enum.KeyCode.LeftShift then
-- If we let shift go and we're running, stop us.
running.Value = false
end
end)
while true do
if reloading then
-- if stamina is full
if stamina.Value >= max_stamina then
reloading = false
else
-- else deplete
stamina.Value = stamina.Value + .5
end
end
wait()
end
connect is depricated use Connect
This discussion is preserved as it was. New answers, comments, and votes are closed.
2 comments
Make sure you have activated the function. Also, make sure you're using UserInputService. Using the mouse for it's access to the keyboard is deprecated.
Only one equal sign is needed, because it isn't a conditional statement (e.g. "if x == y then..."). As for the actual button-down deal, I would imagine ROBLOX has some sort of built-in function to do for that. Unfortunately, I do not know what it is. I couldn't find it in the Object Browser but maybe I missed something? Try the API.