I want to play a sound when a player’s sprint is no more and fade out when the sprinting bar fills back up. I don’t know where to put it in this script nor how to type it. the sound is inside the local script by the way.
Sprinting Script:
local SPRINT_SPEED = 18 -- Your WalkSpeed when sprinting.
local DEFAULT_SPEED = 8 -- Normal speed.
local MAXIMUM_STAMINA = 100
local STAMINA_DECREASE = 0.4
local SPRINT_KEY = Enum.KeyCode.LeftShift
local UserInputService = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Exterior = script.Parent.Exterior
local Bar = Exterior.Bar
local Sprinting = false
local StaminaAmount = MAXIMUM_STAMINA -- StaminaAmount/MAXIMUM_STAMINA
UserInputService.InputBegan:Connect(function(Key, Processed)
if not Processed then
if Key.KeyCode == SPRINT_KEY then
-- Player is sprinting.
Sprinting = true
end
end
end)
UserInputService.InputEnded:Connect(function(Key, Processed)
if not Processed then
if Key.KeyCode == SPRINT_KEY then
-- Player stopped sprinting.
Sprinting = false
end
end
end)
game["Run Service"].RenderStepped:Connect(function()
if StaminaAmount > 0 then -- You have more than 0 stamina in your tank.
if Sprinting then -- If you're actually holding down the key.
if Humanoid.MoveDirection.Magnitude > 0 then
Humanoid.WalkSpeed = SPRINT_SPEED
StaminaAmount -= STAMINA_DECREASE
end
else
Humanoid.WalkSpeed = DEFAULT_SPEED
end
else
Humanoid.WalkSpeed = DEFAULT_SPEED
end
if StaminaAmount < MAXIMUM_STAMINA then
-- do someting.
Exterior.Visible = true
local Formula = (StaminaAmount/MAXIMUM_STAMINA)
Bar:TweenSizeAndPosition(UDim2.new(1, 0, Formula, 0), UDim2.new(0, 0, 1-Formula, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0.1, true)
if not Sprinting then
StaminaAmount += STAMINA_DECREASE
end
else
-- Hide if full.
Exterior.Visible = false
end
end)