Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

I want a loop to run for as long as a button is pressed down, but I have no clue how, help pls?

Asked by 7 years ago

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.

0
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. User#18043 95 — 7y
0
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. MIGHTYMasterChief 3 — 7y

2 answers

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

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)

UserInputService and KeyCodes

Edit: Wrote "Conenct" instead of "Connect"

0
NOTE: set this local script to startergui, starterpack, or starterscripts User#18043 95 — 7y
Ad
Log in to vote
0
Answered by
Azarth 3141 Moderation Voter Community Moderator
7 years ago
-- 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

0
connect is depricated use Connect saSlol2436 716 — 7y

Answer this question