Running animation not playing after death

Hello everyone,

This script controls my sprint system I’ve been working on, which is in StarterCharacterScripts.

It works 100% of the time normally, except after reset where it doesn’t seem to work.

  • meaning that the running/walking changes dont end up in effect at all.

Possible solutions:

  • I read somewhere that this can be changed by adding a Player.CharacterAdded() within the script, but I tried this and it didnt work (at least for me / the way I did it.)

Any help is appreciated
Thanks

-- VARIABLES:

local Player = game.Players.LocalPlayer
local Humanoid = (Player.Character or Player.CharacterAdded:Wait()):FindFirstChild("Humanoid")

local PlayerCam = game.Workspace.Camera

local UIS = game:GetService("UserInputService")

local sprinting = false
-- ANIMATION SETTING: 
local WalkAnim = Instance.new("Animation")
WalkAnim.Name = "WalkAnim" 
WalkAnim.AnimationId = "rbxassetid://82821308814029"

local RunAnim = Instance.new("Animation") 
RunAnim.Name = "RunAnim" RunAnim.AnimationId = "rbxassetid://82072058043542" 

local WalkTrack = Humanoid:LoadAnimation(WalkAnim) 
local RunTrack = Humanoid:LoadAnimation(RunAnim) 

-- TWEENSERVICE CREATION: 
local tweenservice = game:GetService("TweenService") 
local TweenInfo = TweenInfo.new(0.167,Enum.EasingStyle.Sine,Enum.EasingDirection.Out,0,false) 

-- MAIN ANIMATION CONTROL (Fires when player presses control)
local function updateAnimation() 
	-- (If player is moving: )
	if Humanoid.MoveDirection.Magnitude > 0 then
		if sprinting then 
			if not RunTrack.IsPlaying then 
				WalkTrack:Stop()
				RunTrack:Play()
				
			end 
			
		else if not WalkTrack.IsPlaying then 
				RunTrack:Stop()
				WalkTrack:Play() 
				
			end 
		end 
	else
		--If the player ISNT moving, stop both tracks.
		WalkTrack:Stop() 
		RunTrack:Stop()
	end 
end 
-- INPUT DETECTION: 
UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftControl then
		-- If the player isnt sprinting..
		if sprinting == false then 
			sprinting = true 
			-- Fire the animation update function, which plays one track, and turns the other off.
			updateAnimation() 
			-- Sets walkspeed and changes the FOV in the camera
			Player.Character:FindFirstChild("Humanoid").WalkSpeed = 32
			tweenservice:Create(PlayerCam,TweenInfo,{FieldOfView = 80}):Play()
			
		else sprinting = false 
			-- Reverts
			updateAnimation() 
			Player.Character:FindFirstChild("Humanoid").WalkSpeed = 16
			tweenservice:Create(PlayerCam,TweenInfo,{FieldOfView = 70}):Play()
			
		end
	end
end)
-- This below is basically the saviour, if the player stops moving, it updates animation again, which will set both tracks to stop.
Humanoid.Running:Connect(updateAnimation)	
1 Like
LocalScript
--LocalScript in StarterPlayerScripts
local Player = game.Players.LocalPlayer
local UIS = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local PlayerCam = workspace.CurrentCamera

local sprinting = false
local WalkAnim = Instance.new("Animation")
WalkAnim.AnimationId = "rbxassetid://82821308814029"
local RunAnim = Instance.new("Animation")
RunAnim.AnimationId = "rbxassetid://82072058043542"
local TweenInfoObj = TweenInfo.new(0.167, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)

local WalkTrack, RunTrack

local function setupHumanoid(humanoid)
	WalkTrack = humanoid:LoadAnimation(WalkAnim)
	RunTrack = humanoid:LoadAnimation(RunAnim)
	humanoid.Running:Connect(function()
		if humanoid.MoveDirection.Magnitude > 0 then
			if sprinting then
				if not RunTrack.IsPlaying then
					WalkTrack:Stop()
					RunTrack:Play()
				end
			elseif not WalkTrack.IsPlaying then
				RunTrack:Stop()
				WalkTrack:Play()
			end
		else
			WalkTrack:Stop()
			RunTrack:Stop()
		end
	end)
end

local function onCharacter(char)
	local humanoid = char:WaitForChild("Humanoid")
	setupHumanoid(humanoid)
end

Player.CharacterAdded:Connect(onCharacter)
if Player.Character then onCharacter(Player.Character) end

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftControl then
		sprinting = not sprinting
		local humanoid = Player.Character and Player.Character:FindFirstChild("Humanoid")
		if humanoid then humanoid.WalkSpeed = sprinting and 32 or 16 end
		TweenService:Create(PlayerCam, TweenInfoObj, {FieldOfView = sprinting and 80 or 70}):Play()
	end
end)

Just swapped a few thing around and simplified a few things while there.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.