A bit hard to explain so here’s what I mean:
https://gyazo.com/54e984f72e57ef2dd6901d047a471779
Stamina Script:
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()
local UserInputService = game:GetService("UserInputService")
local RS = game:GetService("ReplicatedStorage")
local Stamina = 100
local Running = false
local SprintSpeed = 34
local WalkSpeed = 16
local StaminaLoss = 1
local DiveStaminaLoss = RS.DiveStaminaLoss
DiveStaminaLoss.Event:Connect(function()
if Stamina >= 50 then
Stamina -= 45
end
end)
UserInputService.InputBegan:Connect(function(key)
if key.KeyCode == Enum.KeyCode.LeftShift then
Running = true
Character.Humanoid.WalkSpeed = SprintSpeed
while Stamina > 0 and Running do
if Character.Humanoid.MoveDirection.Magnitude > 0 then
task.wait()
Character["CrouchOrSlide"].Keybinds.Sprint.Value = true
if Stamina <= 1 then
script.Parent.tray.bar.Visible = false
else
script.Parent.tray.bar:TweenSize(UDim2.new(Stamina / 105, 0, .83, 0), "Out", "Linear", 0)
end
wait()
game:GetService("RunService").RenderStepped:Connect(function()
if Stamina <= 0 then
if Character.Humanoid.HipHeight == -1 then
Character.Humanoid.WalkSpeed = 8
else
Character.Humanoid.WalkSpeed = WalkSpeed
Character["CrouchOrSlide"].Keybinds.Sprint.Value = false
end
end
end)
else
task.wait()
Running = false
Character.Humanoid.WalkSpeed = WalkSpeed
Character["CrouchOrSlide"].Keybinds.Sprint.Value = false
while Stamina < 100 and not Running do
script.Parent.tray.bar.Visible = true
Character["CrouchOrSlide"].Keybinds.Sprint.Value = false
--print(Stamina)
script.Parent.tray.bar:TweenSize(UDim2.new(Stamina / 105, 0, .83, 0), "Out", "Linear", 0)
wait()
if Stamina == 0 then
Character.Humanoid.WalkSpeed = WalkSpeed
end
end
end
end
end
end)
UserInputService.InputEnded:Connect(function(key)
if key.KeyCode == Enum.KeyCode.LeftShift then
Running = false
Character.Humanoid.WalkSpeed = WalkSpeed
Character["CrouchOrSlide"].Keybinds.Sprint.Value = false
while Stamina < 104 and not Running do
script.Parent.tray.bar.Visible = true
Character["CrouchOrSlide"].Keybinds.Sprint.Value = false
--print(Stamina)
script.Parent.tray.bar:TweenSize(UDim2.new(Stamina / 105, 0, .83, 0), "Out", "Linear", 0)
wait()
if Stamina == 0 then
Character.Humanoid.WalkSpeed = WalkSpeed
end
end
end
end)
while task.wait() do
if Character.Humanoid.WalkSpeed == 34 then
while Stamina >= 0 and Running do
task.wait(.05)
Stamina -= StaminaLoss
--script.Parent.tray.bar.Visible = true
end
elseif Character.Humanoid.WalkSpeed == 16 or Character.Humanoid.WalkSpeed == 8 then
while Stamina <= 100 and not Running do
task.wait(.05)
Stamina += StaminaLoss
end
end
end
Diving Script (If needed)
local MAX_JUMPS = 1;
local MAX_DIVES = 1;
local TIME_BETWEEN_JUMPS = 0.1;
local hrp = script.Parent:WaitForChild("HumanoidRootPart");
local head = script.Parent:WaitForChild("Head");
local humanoid = script.Parent:WaitForChild("Humanoid");
local RS = game:GetService("ReplicatedStorage")
local DiveStaminaLoss = RS.DiveStaminaLoss
local anim;
if (humanoid.RigType == Enum.HumanoidRigType.R15) then
anim = humanoid:LoadAnimation(script:WaitForChild("Roll_R15"));
else
anim = humanoid:LoadAnimation(script:WaitForChild("Roll_R6"));
end
local canJump = true;
local jumpCount = 0;
local canDive = true;
local diveCount = 0;
local totalDives = 0;
local UIS = game:GetService("UserInputService")
local Keybinds = script.Keybinds
local function onStateChange(old, new)
if (new == Enum.HumanoidStateType.Landed or new == Enum.HumanoidStateType.Swimming or new == Enum.HumanoidStateType.Running or new == Enum.HumanoidStateType.RunningNoPhysics) then
canDive = true;
canJump = true;
jumpCount = 0;
diveCount = 0;
anim:Stop();
elseif (new == Enum.HumanoidStateType.Freefall or new == Enum.HumanoidStateType.Flying) then
wait(TIME_BETWEEN_JUMPS)
canJump = true;
canDive = true;
end
end
local function onJumpRequest()
if (not humanoid or humanoid:GetState() == Enum.HumanoidStateType.Dead) then
return;
end
if (canJump and jumpCount < MAX_JUMPS) then
canJump = false;
jumpCount = jumpCount + 1;
humanoid:ChangeState(Enum.HumanoidStateType.Jumping);
if (jumpCount > 1) then
if (jumpCount % 2 == 0 or humanoid:GetState() == Enum.HumanoidStateType.Flying) then
anim:Play(nil, nil, 2.5);
end
local z = hrp.CFrame:vectorToObjectSpace(hrp.Velocity).z;
end
end
end
local function onInput(input, process)
if (process or not humanoid or humanoid:GetState() == Enum.HumanoidStateType.Dead) then
return;
end
if (input.KeyCode == Enum.KeyCode.Space and humanoid:GetState() == Enum.HumanoidStateType.Freefall and canDive and diveCount < MAX_DIVES) then
canDive = false;
diveCount = diveCount + 1;
totalDives = totalDives + 1;
anim:Stop();
humanoid:ChangeState(Enum.HumanoidStateType.Flying);
DiveStaminaLoss:Fire()
if script.Parent.States.Slide.Value == false then
if Keybinds.W.Value == true then
hrp.RotVelocity = hrp.CFrame:vectorToWorldSpace(Vector3.new(-math.rad(135), 0, 0));
hrp.Velocity = hrp.CFrame:vectorToWorldSpace(Vector3.new(0, humanoid.JumpPower, -humanoid.WalkSpeed*2))
script.CoilSound:Play()
elseif Keybinds.S.Value == true then
hrp.RotVelocity = hrp.CFrame:vectorToWorldSpace(Vector3.new(math.rad(135), 0, 0));
hrp.Velocity = hrp.CFrame:vectorToWorldSpace(Vector3.new(0, humanoid.JumpPower, humanoid.WalkSpeed*2))
script.CoilSound:Play()
elseif Keybinds.D.Value == true then
hrp.RotVelocity = hrp.CFrame:vectorToWorldSpace(Vector3.new(0, 0, -math.rad(90)));
hrp.Velocity = hrp.CFrame:vectorToWorldSpace(Vector3.new(humanoid.WalkSpeed*2, humanoid.JumpPower, 0))
script.CoilSound:Play()
elseif Keybinds.A.Value == true then
hrp.RotVelocity = hrp.CFrame:vectorToWorldSpace(Vector3.new(0, 0, math.rad(90)));
hrp.Velocity = hrp.CFrame:vectorToWorldSpace(Vector3.new(-humanoid.WalkSpeed*2, humanoid.JumpPower, 0))
script.CoilSound:Play()
end
end
local currentDive = totalDives;
wait(0.5);
if (currentDive == totalDives) then
hrp.RotVelocity = hrp.CFrame:vectorToWorldSpace(Vector3.new(0, 0, 0));
end
end
end
local function onTouched(touchingPart, humanoidPart)
if (humanoid:GetState() == Enum.HumanoidStateType.Flying and touchingPart.CanCollide) then
local ray = Ray.new(humanoidPart.Position, -humanoidPart.Velocity * 10);
local hit, pos, normal, material = game.Workspace:FindPartOnRayWithWhitelist(ray, {touchingPart});
if (material ~= Enum.Material.Water and material ~= Enum.Material.Air and normal:Dot(Vector3.new(0, 1, 0)) <= 0.5) then
hrp.Velocity = (normal + Vector3.new(0, 1, 0)) * 30;
anim:Play(nil, nil, 2.5);
end
humanoid:ChangeState(Enum.HumanoidStateType.Freefall);
end
end
if (MAX_DIVES > 0) then
humanoid:SetStateEnabled(Enum.HumanoidStateType.FallingDown, false);
humanoid.Touched:Connect(onTouched);
game:GetService("UserInputService").InputBegan:Connect(onInput);
end
humanoid.StateChanged:Connect(onStateChange);
game:GetService("UserInputService").JumpRequest:Connect(onJumpRequest);