Stamina Code Review

This is a stamina script, Im currently trying to make it so when you jump it takes away


local TweeningService = game:GetService("TweenService")

local UIS = game:GetService('UserInputService')

local Bar = script.Parent

local player = game.Players.LocalPlayer

local NormalWalkSpeed = 16
local NewWalkSpeed = 18

local power = 10

local sprinting = false

repeat wait() until game.Players.LocalPlayer.Character

local character = player.Character

--= Tween Functions =--

-- sprinting

        local FOVChanges = {
	        FieldOfView = 90
        }
        local TweenInformation = TweenInfo.new(
	        1, --tween length
	        Enum.EasingStyle.Quint, --easing Style
	        Enum.EasingDirection.Out, --easing Direction
	        0, --repitition time
	        false, --reverse?
	        0 --delay				
        )

        local tween = TweeningService:Create(camera,TweenInformation,FOVChanges)

-- walking

        local FOVChanges2 = {
	        FieldOfView = 70
        }
        local TweenInformation2 = TweenInfo.new(
	        1, --tween length
	        Enum.EasingStyle.Quint, --easing Style
	        Enum.EasingDirection.Out, --easing Direction
	        0, --repitition time
	        false, --reverse?
	        0 --delay				
        )
 
        local tween2 = TweeningService:Create(camera,TweenInformation2,FOVChanges2)

--= Functions =--

UIS.InputBegan:connect(function(key, gameProcessed)
	if key.KeyCode == Enum.KeyCode.LeftShift and gameProcessed == false then
		character.Humanoid.WalkSpeed = NewWalkSpeed
		sprinting = true
		while power > 0 and sprinting do
			power = power - .03
			Bar.Size = UDim2.new(power / 10, 0, 1, 0)
			--Bar.BackgroundColor3 = Bar.BackgroundColor3:lerp(Color3.fromRGB(255, 42, 42), 0.001)
			tween:Play()
			wait()
			if power <= 0 then
				character.Humanoid.WalkSpeed = NormalWalkSpeed
			end
		end
	end
end)

UIS.InputEnded:connect(function(key, gameProcessed)
	if key.KeyCode == Enum.KeyCode.LeftShift and gameProcessed == false then
		character.Humanoid.WalkSpeed = NormalWalkSpeed
		sprinting = false
		while power < 10 and not sprinting do
			power = power + .03
			Bar.Size = UDim2.new(power / 10, 0, 1, 0)
			--Bar.BackgroundColor3 = Bar.BackgroundColor3:lerp(Color3.fromRGB(255, 166, 11), 0.001)
			tween2:Play()
			wait()
			if power <= 0 then
				character.Humanoid.WalkSpeed = NormalWalkSpeed
			end
		end
	end
end)
3 Likes

Variable inconsistency

local TweenService = game:GetService("TweenService")
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")

Alternatively, you could just do

local Character = player.Character or player.CharacterAdded:Wait()

It combines the 2 lines into 1


The stuff inside of the UIS event functions can be made modular so you don’t keep on using similar code too many times.

2 Likes

I don’t know why you unnecessarily indented two random chunks of your code, but it doesn’t matter i guess. You should be using :Connect() instead of :connect() as :Connect() isn’t deprecated, while :connect() is.

You should move to another service, the ContextActionService. Here is why:

3 Likes