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

How to remove stamina when jumping?

Asked by 4 years ago

So I've made a stamina script in which stamina will be decreased as you sprint, however I also want to add a feature in which that when you jump, a block of stamina will be removed. I have no clue how to incorporate it into my script and help would highly be appreciated.

local players = game:GetService("Players")
local inputService = game:GetService("UserInputService")

local player = players.LocalPlayer
local char = player.Character or player.CharacterAdded:wait()
local humanoid = char:WaitForChild("Humanoid")

local stamina = 100

local Interface = {}
do
    local playerGui = player:WaitForChild("PlayerGui")

    local gui = playerGui:WaitForChild("Stamina")
    local background = gui:WaitForChild("Background")
    local bar = background:WaitForChild("Bar")

    local UDim2_new = UDim2.new

    function Interface:UpdateStamina()
        bar.Size = UDim2_new(stamina/100, 0, 1, 0)
    end
end

inputService.InputBegan:connect(function(input, event)
    if (event or stamina <= 0) then return end

    local key = input.KeyCode.Name

    if (key == "LeftControl") then
        humanoid.WalkSpeed = 24
        repeat
            if (humanoid.MoveDirection.Magnitude > 0.1) then
                stamina = stamina - 1
                Interface:UpdateStamina()
            end
            wait(0)
        until not inputService:IsKeyDown(Enum.KeyCode.LeftControl) or stamina <= 0

        humanoid.WalkSpeed = 16

        if (stamina <= 0) then
            wait(2)
        else
            wait(1)
        end

        while (not inputService:IsKeyDown(Enum.KeyCode.LeftControl) and stamina < 100) do
            stamina = stamina + 2
            Interface:UpdateStamina()
            wait(0)
        end
    end
end)

2 answers

Log in to vote
0
Answered by 4 years ago

try this:

humanoid.Jumped:Connect(function(active)
    if active then
        --remove stamina
    end
end)
0
reeeeee, people spoon feeding answers. Explain your answer!!!! BashGuy10 384 — 4y
0
Srry im new to the forum. Dudeguy90539 36 — 4y
0
The Humanoid.Jumped event fires whenever the humanoid jumps. Using this, you can deduct stamina from the player whenever the humanoid *jumps*, and not just with the spacebar (spamming would cause stamina loss without an actual jump). for a full explanation of this event, here is a link to the wiki page: https://developer.roblox.com/en-us/api-reference/event/Humanoid/Jumping Dudeguy90539 36 — 4y
Ad
Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

@Dudeguy90539's answer is not correct, and does not work. This is how you do it:

humanoid.Jumping:Connect(function(isActive)
    print("Jumping: ", isActive)
    if isActive then -- lower stamina if jumping == true
        stamina = stamina - 25 -- edit this value
    end
end)

This code, when run from a LocalScript parented to a Player.Character will listen to the Humanoid’s Jumping action event and print when it is fired. If this helped you figure it out, please mark it as the answer, ty :)

1
XD XD I meant jumping XD XD Dudeguy90539 36 — 4y
0
ty for catching that XD XD Dudeguy90539 36 — 4y

Answer this question