My code works fine the first try but after I reset my character it breaks
local PlrService = game:GetService("Players")
local Uis = game:GetService("UserInputService")
local Ts = game:GetService("TweenService")
local RS = game:GetService("RunService")
local Ti = TweenInfo.new(1,Enum.EasingStyle.Cubic,Enum.EasingDirection.Out,0,false,0)
local Plr = PlrService.LocalPlayer
local Character = Plr.CharacterAdded:Wait() or Plr.Character
local Humanoid = Character:WaitForChild("Humanoid")
local Cam = workspace.CurrentCamera
local Anim = script:WaitForChild("SprintAnim")
local AnimTrack = Humanoid:LoadAnimation(Anim)
local IsSprinting = false
local Moving = false
local SprintSpeed = 30
local WalkSpeed = 12
local SprintFOV = 85
local WalkFOV = Cam.FieldOfView
local Rs = game.ReplicatedStorage
local PlayerGui = Plr.PlayerGui
local Stamina = PlayerGui:WaitForChild("PlayerInterfaceGUI").StaminaBackground.StaminaBar
local Tick = math.huge
local Toggle = false
local Toggle2 = false
local function Sprint(TOGGLE,ISSPRINTING,FOV,WS)
Toggle = TOGGLE
IsSprinting = ISSPRINTING
Ts:Create(Cam,Ti,{FieldOfView = FOV}):Play()
Humanoid.WalkSpeed = WS
end
Cam.Changed:Connect(function(prop)
if prop == "FieldOfView" then
if IsSprinting == true then return end
SprintFOV = Cam.FieldOfView + 15
WalkFOV = 70
end
end)
Rs:WaitForChild("Remotes"):WaitForChild("PowerUp").OnClientEvent:Connect(function(PowerUp)
if PowerUp == "SpeedBoostPowerup" then
SprintSpeed = 60
WalkSpeed = 24
wait(5)
SprintSpeed = 30
WalkSpeed = 12
elseif PowerUp == "JumpBoostPowerup" then
SprintSpeed = 60
WalkSpeed = 24
Humanoid.JumpPower = 100
wait(5)
SprintSpeed = 30
WalkSpeed = 12
Humanoid.JumpPower = 50
end
if IsSprinting == true then
Sprint(true,true,SprintFOV,SprintSpeed)
elseif IsSprinting == false then
Sprint(false,false,WalkFOV,WalkSpeed)
end
wait(5.1)
if IsSprinting == true then
Sprint(true,true,SprintFOV,SprintSpeed)
elseif IsSprinting == false then
Sprint(false,false,WalkFOV,WalkSpeed)
end
end)
Uis.InputBegan:Connect(function(Input, GameProcessed)
if GameProcessed then return end
if Moving == false then return end
if Input.KeyCode == Enum.KeyCode.LeftShift then
if Toggle == false and Plr:GetAttribute("Stamina") ~= 0 then
Sprint(true,true,SprintFOV,SprintSpeed)
AnimTrack:Play()
elseif Toggle == true then
Sprint(false,false,WalkFOV,WalkSpeed)
AnimTrack:Stop()
end
end
end)
Humanoid.Changed:Connect(function(prop)
if prop == "MoveDirection" then
if Humanoid.MoveDirection == Vector3.new(0,0,0) then
Moving = false
Sprint(false,false,WalkFOV,WalkSpeed)
AnimTrack:Stop()
end
end
end)
Humanoid.Changed:Connect(function(prop)
if prop == "MoveDirection" then
if Humanoid.MoveDirection ~= Vector3.new(0,0,0) then
Moving = true
end
end
end)
RS.Heartbeat:Connect(function()
if Toggle == true and Moving == true then
Plr:SetAttribute("Stamina",Plr:GetAttribute("Stamina") - Plr:GetAttribute("DecreaseSprintSpeed"))
Tick = os.time()
else
if os.time()-Tick >= 3 then
Plr:SetAttribute("Stamina",Plr:GetAttribute("Stamina") + Plr:GetAttribute("RegenWalkSpeed"))
if Plr:GetAttribute("Stamina") >= 99.5 then
Plr:SetAttribute("Stamina",100)
end
end
end
if Plr:GetAttribute("Stamina") == 0 then
Sprint(false,false,WalkFOV,WalkSpeed)
AnimTrack:Stop()
end
end)