Slide and Jump isn't decreasing Stamina

hello, I was working on my stamina system and I wanted it to decrease stamina by 20 if you press q and decrease stamina by 10 if they jump, I tried to do this but it didn’t work, only using shift works for sprinting

local UIS = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid" ,5)
local StaminaUsed = script:WaitForChild("StaminaUsed" ,5)
local Keybind = "LeftShift"
local MaxStamina = 100
local NormalWalkspeed = 16
local SprintWalkspeed = 25

anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://8107329636"
animPlay = Humanoid:LoadAnimation(anim)

local StaminaCount = MaxStamina
local SlideUsed = script.Slided
local JumpUsed = script.Jumped

local breathing = script.Parent["Heavy Breathing ( Man )"]

UIS.InputBegan:Connect(function(key, processed)
	if key.UserInputType == Enum.UserInputType.Keyboard then
		if key.KeyCode == Enum.KeyCode[Keybind] and StaminaCount > 0 then
			StaminaUsed.Value = true
		end
	end
end)
UIS.InputEnded:Connect(function(key, processed)
	if key.UserInputType == Enum.UserInputType.Keyboard then
		if key.KeyCode == Enum.KeyCode[Keybind] then
			StaminaUsed.Value = false
		end
	end
end)

StaminaUsed.Changed:Connect(function()
	if StaminaUsed.Value == true and Humanoid.MoveDirection.Magnitude > 0 then
		Humanoid.WalkSpeed = SprintWalkspeed
		animPlay:Play()
		for i = 1,5 do
			game.Workspace.CurrentCamera.FieldOfView = (70+(i*2))
			wait()
		end
		repeat
			wait(0.1)
			if StaminaCount > 0 then
				StaminaCount = StaminaCount - 1
				script.Parent.Frame.Size = UDim2.new(StaminaCount / MaxStamina, 0, 1, 0)
				script.Parent.Info.Text = StaminaCount.. "/" ..MaxStamina
			end
		until
		StaminaCount <= 0 or StaminaUsed.Value == false
		Humanoid.WalkSpeed = NormalWalkspeed
	elseif StaminaUsed.Value == false then
		Humanoid.WalkSpeed = NormalWalkspeed
	    animPlay:Stop()
		breathing.Playing = false
		for i = 1,5 do
			game.Workspace.CurrentCamera.FieldOfView = (80-(i*2)) -- make FOV smaller
			wait()
		end
		repeat
			wait(0.1)
			if StaminaCount < MaxStamina then
				StaminaCount = StaminaCount + 1
				script.Parent.Frame.Size = UDim2.new(StaminaCount / MaxStamina, 0, 1, 0)
				script.Parent.Info.Text = StaminaCount.. "/" ..MaxStamina
			end
		until
		StaminaCount == MaxStamina or StaminaUsed.Value == true
	end
end)

UIS.InputBegan:Connect(function(input,gameProcessed)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.Q and StaminaCount > 0 then
			SlideUsed.Value = true
		end
	end
end)

UIS.InputBegan:Connect(function(input,gameProcessed)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.Q and StaminaCount > 0 then
			JumpUsed.Value = true
		end
	end
end)

SlideUsed.Changed:Connect(function()
	local deb2 = true
	if SlideUsed.Value == true then
		StaminaCount = StaminaCount - 20
	end
end)

JumpUsed.Changed:Connect(function()
	if JumpUsed.Value == true then
		if StaminaCount > 0 then
			StaminaCount = StaminaCount - 10
			script.Parent.Frame.Size = UDim2.new(StaminaCount / MaxStamina, 0, 1, 0)
			script.Parent.Info.Text = StaminaCount.. "/" ..MaxStamina
		end
	end
end)
1 Like

You set SlideUsed and JumpUsed to true in your events, however you haven’t set one for setting them to false.
Secondly, your Jump event is checking against Q being pressed, rather than the Spacebar.

Just to clear up your code a bit, you can combine your Jump, slide and sprint user input events into one.

UIS.InputBegan:Connect(function(key, processed)
	if key.UserInputType == Enum.UserInputType.Keyboard then
		if key.KeyCode == Enum.KeyCode[Keybind] and StaminaCount > 0 then
			StaminaUsed.Value = true
		elseif key.KeyCode == Enum.KeyCode.Q and StaminaCount > 0 then
			SlideUsed.Value = true
		elseif key.KeyCode == Enum.KeyCode.Space and StaminaCount > 0 then
			JumpUsed.Value = true
		end
	end
end)
UIS.InputEnded:Connect(function(key, processed)
	if key.UserInputType == Enum.UserInputType.Keyboard then
		if key.KeyCode == Enum.KeyCode[Keybind] then
			StaminaUsed.Value = false
		elseif key.KeyCode == Enum.KeyCode.Q then
			SlideUsed.Value = false
		elseif key.KeyCode == Enum.KeyCode.Space then
			JumpUsed.Value = false
		end
	end
end)

You could even condense this further by passing both InputBegan and Ended off to another function

function UserInput(io,processed)
	--If InputBegan - TRUE, otherwise FALSE
	local inputBegin = io.UserInputState == Enum.UserInputState.Begin

	if io.UserInputType == Enum.UserInputType.Keyboardthen
		if io.KeyCode == Enum.KeyCode[Keybind]  then
			StaminaUsed.Value = inputBegin and  and StaminaCount > 0  --if StaminaCount or inputBegin is FALSE, set these to false.
		elseif io.KeyCode == Enum.KeyCode.Q  then
			SlideUsed.Value = inputBegin and  and StaminaCount > 0 
		elseif io.KeyCode == Enum.KeyCode.Space  then
			JumpUsed.Value = inputBegin  and StaminaCount > 0 
		end
	end
end


UIS.InputBegan:Connect(UserInput)
UIS.InputEnded:Connect(UserInput)
2 Likes

Thank you very much for clearing this up for me and even cleaning up my code, may I ask where you learned this ( I am not as Advanced as you )

I’m pretty much self taught.
Been working on Roblox since 2009 and have learned a good amount of stuff since then from other people’s as well as my own experiments and mistakes.

With programming you’ve got to be open to the idea of making mistakes, and then solving those mistakes. That’s how you learn what works.

Keep experimenting. But enjoy it as well. :grinning:

1 Like