im using a configrated version of a running system. its simple system for running and works all fine!
but problem is if characters dies once. when player tries to run again animation plays but speed and fov doesnt change at all, how can i fix it?
script:
-- Settings
local sprintSpeed = 27
local defaultSpeed = 12.5
local sprintFov = 80
local normalFov = 70
local sprintKey = Enum.KeyCode.LeftShift
local sprintButton = Enum.KeyCode.ButtonL3
--// Services
-- External services
local userInputService = game:GetService("UserInputService")
local tweenService = game:GetService("TweenService")
local runService = game:GetService("RunService")
-- Internal services
local replicatedStorage = game:GetService("ReplicatedStorage")
local playersService = game:GetService("Players")
-- // Variables
-- Comms variables
local sprintEvent = replicatedStorage:WaitForChild("SprintEvent")
-- Player variables
local player = playersService.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local camera = workspace.CurrentCamera
-- Animation variables
local sprintAnim = script:WaitForChild("SprintAnim")
local sprintTrack = humanoid:LoadAnimation(sprintAnim)
-- Booleans
local sprinting = false
-- Functions
local function tweenFov(duration, fov)
local tween = tweenService:Create(camera, TweenInfo.new(duration, Enum.EasingStyle.Quad), {FieldOfView = fov})
tween:Play()
spawn(function()
tween.Completed:Wait()
tween:Destroy()
end)
end
-- // Events
-- Input handling
userInputService.InputBegan:Connect(function(input, processed)
if input.KeyCode == sprintKey and not processed then
if not sprinting then
sprinting = true
if humanoid.MoveDirection.Magnitude > 0 then
if not sprintTrack.IsPlaying then
sprintTrack:Play(0.25)
else if humanoid.MoveDirection.Magnitude <= 0 then
sprintTrack:Play(0.25)
wait(0.3)
sprintTrack:Stop()
end
end
end
if sprintTrack.IsPlaying then
sprintEvent:FireServer(sprintSpeed)
tweenFov(0.35, sprintFov)
end
end
repeat
task.wait()
if humanoid.MoveDirection.Magnitude <= 0 then
sprintTrack:Stop()
sprinting = false
sprintEvent:FireServer(defaultSpeed)
tweenFov(0.35, normalFov)
end
until false
elseif input.KeyCode == sprintButton and not processed then
if sprinting then
sprinting = false
if sprintTrack.IsPlaying then
sprintTrack:Stop()
end
sprintEvent:FireServer(defaultSpeed)
tweenFov(0.35, normalFov)
else
sprinting = true
if humanoid.MoveDirection.Magnitude > 0 then
if not sprintTrack.IsPlaying then
sprintTrack:Play(0.25)
else if humanoid.MoveDirection.Magnitude <= 0 then
sprintTrack:Play(0.25)
wait(0.3)
sprintTrack:Stop()
end
end
end
if sprintTrack.IsPlaying then
sprintEvent:FireServer(sprintSpeed)
tweenFov(0.35, sprintFov)
end
end
end
repeat
task.wait()
if humanoid.MoveDirection.Magnitude <= 0 then
sprintTrack:Stop()
sprinting = false
sprintEvent:FireServer(defaultSpeed)
tweenFov(0.35, normalFov)
end
until false
end)
userInputService.InputEnded:Connect(function(input)
if input.KeyCode == sprintKey then
sprinting = false
if sprintTrack.IsPlaying then
sprintTrack:Stop()
end
sprintEvent:FireServer(defaultSpeed)
tweenFov(0.35, normalFov)
end
end)
-- Animation handling
humanoid:GetPropertyChangedSignal("FloorMaterial"):Connect(function()
if humanoid.FloorMaterial == Enum.Material.Air then
if sprinting then
sprinting = false
repeat wait()
if sprintTrack.IsPlaying then
sprintTrack:Stop()
end
until humanoid.FloorMaterial ~= Enum.Material.Air
if sprinting then
if humanoid.MoveDirection.Magnitude > 0 then
if not sprintTrack.IsPlaying then
sprintTrack:Play(0.25)
else if humanoid.MoveDirection.Magnitude < 2 then
sprintTrack:Play(0.25)
wait(0.3)
sprintTrack:Stop()
sprinting = false
end
end
end
else
sprinting = false
if sprintTrack.IsPlaying then
sprintTrack:Stop()
end
sprintEvent:FireServer(defaultSpeed)
tweenFov(0.35, normalFov)
end
end
else
if sprinting then
if humanoid.MoveDirection.Magnitude > 0 then
if not sprintTrack.IsPlaying then
sprintTrack:Play(0.25)
else if humanoid.MoveDirection.Magnitude < 2 then
sprintTrack:Play(0.25)
wait(0.3)
sprintTrack:Stop()
sprinting = false
end
end
end
end
end
end)
humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
if humanoid.MoveDirection.Magnitude > 0 then
if sprinting then
if not sprintTrack.IsPlaying then
sprintTrack:Play(0.25)
else if humanoid.MoveDirection.Magnitude <= 0 then
if sprinting then
sprintTrack:Play(0.25)
wait(0.3)
sprintTrack:Stop()
end
end
end
end
end
end)
humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
if humanoid.MoveDirection.Magnitude <= 0 then
sprinting = false
sprintEvent:FireServer(defaultSpeed)
tweenFov(0.35, normalFov)
end
end)
while true do
task.wait()
if sprinting then
if not sprintTrack.IsPlaying then
sprintTrack:Play(0.25)
else
local Pos = character.HumanoidRootPart.Position
--print(Pos)
wait(0.3)
local Pos2 = character.HumanoidRootPart.Position
--print(Pos2)
if Pos == Pos2 then
sprintTrack:Stop()
sprinting = false
sprintEvent:FireServer(defaultSpeed)
tweenFov(0.35, normalFov)
end
end
end
end
Script of WalkSpeedHandler at serverscriptservice
-- // Services
-- Internal services
local replicatedStorage = game:GetService("ReplicatedStorage")
local playersService = game:GetService("Players")
-- // Variables
-- Comms variables
local sprintEvent = replicatedStorage:WaitForChild("SprintEvent")
-- // Events
-- Comms events
sprintEvent.OnServerEvent:Connect(function(player, walkSpeed)
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
humanoid.WalkSpeed = walkSpeed
end)