Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How can I add JumpPower to Stamina Bar?

Hello, so I am attempting to create this stamina bar script. However, I have the sprinting part completed and tested. However, I want to incorporate the jumping power into the stamina bar. What I mean by this is that I want it when the player jumps it subtracts 10 from the stamina bar GUI. I'm afraid that when the player sprints it might screw up the stamina bar. I'm basically creating a limited sprint and jump script. Also the stamina bar glitches sometimes when I sprint as it goes out of the bar when regening my stamina.

Stamina Script:

local CAS = game:GetService("ContextActionService")
local RE = game:GetService("ReplicatedStorage"):WaitForChild("RE")
local fill = script.Parent:WaitForChild("top"):WaitForChild("fill")

local stam = 50
local speed = 16
local sprinting = false
local Jumped = false
local player = game.Players.LocalPlayer
local Char = player.Character
local UIS = game:GetService('UserInputService')

Char:FindFirstChild('Humanoid').JumpPower = 0

UIS.InputBegan:connect(function(input)
	if input.KeyCode == Enum.KeyCode.Space and not Jumped then
		Jumped = true
		Char:FindFirstChild('Humanoid').JumpPower = 50
		wait(.2)
		Char:FindFirstChild('Humanoid').JumpPower = 0
		wait(5)
		Jumped = false
	end
end)

local function sprint(str, state, object)
	if stam < 1 then return end
	speed = state == Enum.UserInputState.Begin and 24 or 16
	sprinting = state == Enum.UserInputState.Begin
	while sprinting and stam > 0 and wait(.1) do
		stam = stam - 1
		fill:TweenSize(UDim2.new(stam/50, 0, 1, 0), "Out", "Sine", .1, true)
		RE:FireServer('sprint', {speed})
	end
	sprinting = false
	speed = 16
	RE:FireServer('sprint', {speed})
	wait(1)
	while not sprinting and stam < 50 and wait(2) do
		fill:TweenSize(UDim2.new(stam/50, 0, 1, 0), "Out", "Sine", .1, true)
		stam = stam + 1
	end
end

CAS:BindAction("sprint", sprint, true, Enum.KeyCode.LeftShift)

Please help me, thank you!

1 answer

local CAS = game:GetService("ContextActionService")
local RE = game:GetService("ReplicatedStorage"):WaitForChild("RE")
local fill = script.Parent:WaitForChild("top"):WaitForChild("fill")

local stam = 50
local speed = 16
local sprinting = false
local Jumped = false
local player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait() -- Gets local player or waits for the object to load
local Char = player.Character or player.CharacterAdded:Wait() -- Gets character or waits for the object to load
local UIS = game:GetService("UserInputService")

Char:FindFirstChild('Humanoid').JumpPower = 0

UIS.InputBegan:connect(function(input)
    if input.KeyCode == Enum.KeyCode.Space and not Jumped then
        Jumped = true
        Char:FindFirstChild('Humanoid').JumpPower = 50
        wait(.2)
        Char:FindFirstChild('Humanoid').JumpPower = 0
        wait(5)
        Jumped = false
    end
end)

local function sprint(str, state, object)
    if stam < 1 then return end
    speed = state == Enum.UserInputState.Begin and 24 or 16
    sprinting = state == Enum.UserInputState.Begin
    while sprinting and stam > 0 and wait(.1) do
        stam = stam - 1
        fill:TweenSize(UDim2.new(stam/50, 0, 1, 0), "Out", "Sine", 0.1, true)
        RE:FireServer("sprint", {speed})
    end
    sprinting = false
    speed = 16
    RE:FireServer("sprint", {speed})
    wait(1)
    while not sprinting and stam < 50 and (wait(2))do -- I feel like the bug is here
        fill:TweenSize(UDim2.new(stam/50, 0, 1, 0), "Out", "Sine", 0.1, true)
        stam = stam + 1 
    end
end
local function jumping (<args>)
	-- Code here -- 
end
character:FindFirstChild("Humanoid").Jumping:Connect(jumping) -- The first argument for the Jumping event is a boolean[if its jumping]

CAS:BindAction("sprint", sprint, true, Enum.KeyCode.LeftShift)

You use the humanoid.Jumping event Please accept my answer if it helped you in any way fix your code

2 comments

0

The stamina bar still goes outside of the barrier. Also I don't know how I can add the jumping affect the stamina bar.

0

You use the humanoid.Jump event

This discussion is preserved as it was. New answers, comments, and votes are closed.