Hello again.
Currently, I am creating a stamina System, basic one, but the problem is that they are 2 scripts:
First Script, The MainMovement Script:
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")::Humanoid
local RootPart = Character:WaitForChild("HumanoidRootPart")::Part
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local SlowSpeed = 8
local WalkSpeed = 16
local RunSpeed = 30
local CurrentSpeed = WalkSpeed
local IsSlowWalking = false
local IsSpaceBugSpeed = false
local GroundAccel = 16
local GroundDecel = 12
local AirAccel = 15
local PlayerVelocity = Vector3.zero
local LinearVelocity = Instance.new("LinearVelocity",RootPart)
LinearVelocity.Attachment0 = RootPart:WaitForChild("RootAttachment")
LinearVelocity.MaxForce = 25500
LinearVelocity.RelativeTo = Enum.ActuatorRelativeTo.World
LinearVelocity.VelocityConstraintMode = Enum.VelocityConstraintMode.Plane
LinearVelocity.PrimaryTangentAxis = Vector3.new(1,0,0)
LinearVelocity.SecondaryTangentAxis = Vector3.new(0,0,1)
function Move(direction, targetSpeed, rate1, rate2)
local CurrentVelocity = PlayerVelocity
local TargetVelocityMagnitude = direction * targetSpeed
local Add = (TargetVelocityMagnitude - CurrentVelocity)
local Rate = if CurrentVelocity.Magnitude < targetSpeed or direction.Magnitude > 0 then rate1 else rate2
PlayerVelocity += Add * Rate * RunService.Heartbeat:Wait()
end
local Connection = RunService.RenderStepped:Connect(function(dt)
for i, v in Character:GetChildren() do
if v:IsA("BasePart") then
v.CustomPhysicalProperties = PhysicalProperties.new(.7,0,0,100,100)
end
end
Humanoid.WalkSpeed = CurrentSpeed
Humanoid.UseJumpPower = true
local Grounded = Humanoid:GetState() ~= Enum.HumanoidStateType.Freefall
if Grounded and not Humanoid.Jump then
Move(Humanoid.MoveDirection, CurrentSpeed, GroundAccel, GroundDecel)
end
if not (Humanoid:GetState() == Enum.HumanoidStateType.Climbing) then
LinearVelocity.PlaneVelocity = Vector2.new(PlayerVelocity.X, PlayerVelocity.Z)
else
LinearVelocity.PlaneVelocity = Vector2.zero
PlayerVelocity = Vector3.zero
end
end)
UserInputService.InputBegan:Connect(function(input, p)
if p then return end
if input.KeyCode == Enum.KeyCode.LeftShift or input.KeyCode == Enum.KeyCode.ButtonR1 then
if Humanoid.MoveDirection.Magnitude > 0 then
CurrentSpeed = RunSpeed
end
elseif input.KeyCode == Enum.KeyCode.V or input.KeyCode == Enum.KeyCode.ButtonR2 then
IsSlowWalking = not IsSlowWalking
if IsSlowWalking then
CurrentSpeed = SlowSpeed
else
CurrentSpeed = WalkSpeed
end
end
end)
UserInputService.InputEnded:Connect(function(input, p)
if p then return end
if input.KeyCode == Enum.KeyCode.LeftShift or input.KeyCode == Enum.KeyCode.ButtonR1 then
CurrentSpeed = WalkSpeed
end
end)
Second Script, The StaminaBar Script:
local UIS = game.UserInputService
local TweenService = game.TweenService
local BarInfo = TweenInfo.new(0.3, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut, 0, false, 0)
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local BackGroundBar = script.Parent.Parent
local Bar = script.Parent
local Stamina = 250
local IsRunning = false
Stamina = math.clamp(Stamina, 0, 250)
local function updateBarVisibility()
if IsRunning and Character.Humanoid.MoveDirection.Magnitude > 0 then
TweenService:Create(BackGroundBar, BarInfo, {BackgroundTransparency = 0}):Play()
TweenService:Create(Bar, BarInfo, {BackgroundTransparency = 0}):Play()
else
TweenService:Create(BackGroundBar, BarInfo, {BackgroundTransparency = 1}):Play()
TweenService:Create(Bar, BarInfo, {BackgroundTransparency = 1}):Play()
end
end
local function regenerateStamina()
while Stamina < 250 and not IsRunning do
Stamina = Stamina + 0.3
script.Parent:TweenSize(UDim2.new(Stamina / 250, 0, 1, 0), "InOut", "Quad", 0)
task.wait()
end
end
UIS.InputBegan:Connect(function(input, p)
if p then return end
if input.KeyCode == Enum.KeyCode.LeftShift then
if Character.Humanoid.MoveDirection.Magnitude > 0 then
IsRunning = true
updateBarVisibility()
while Stamina > 0 and IsRunning do
Stamina = Stamina - 0.1
script.Parent:TweenSize(UDim2.new(Stamina / 250, 0, 1, 0), "InOut", "Quad", 0)
task.wait()
if Stamina <= 0 then
IsRunning = false
end
end
end
end
end)
UIS.InputEnded:Connect(function(input, p)
if p then return end
if input.KeyCode == Enum.KeyCode.LeftShift then
IsRunning = false
task.wait(1)
updateBarVisibility()
regenerateStamina()
end
end)
Now, the problem is the stamina, isn’t staminaing it. I mean, those 2 scripts are not in 1. If the stamina bar is 0/ the bar is disappeared , the player can still run. I only need to fix this bug, can someone help me?
Best, XMLcard