Play a stop animation when stop running

hey
I’m trying to make a script then when the player stop walking while running
the character play a stop animation
but i click shift they already play the stop animation
i try to add some debaunces but dint work and idk what to do now

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local RootPart = Character:WaitForChild("HumanoidRootPart")
local mouse = Player:GetMouse()
local Camera = workspace.CurrentCamera
local StarterGui = Player:WaitForChild("PlayerGui")

local UserInputService = game:GetService("UserInputService")
local runService = game:GetService("RunService")

local FelledAnim = Instance.new("Animation")
FelledAnim.AnimationId = "rbxassetid://14893749899"

local RunningAnim = Instance.new("Animation")
RunningAnim.AnimationId = "rbxassetid://14893142590"

local StopRunning = Instance.new("Animation")
StopRunning.AnimationId = "rbxassetid://14967979813"

local db = false
local jumping = false
local falling = false
local running = false
local runningdb = false
local climbing = false
local locked = false
local LPlayer = nil
local onUi = false
local canrough = false
local roughdb = false

local RunAnimHandler

local walkingtime = tick()

Humanoid.Died:Connect(function()
	db = false
	jumping = false
	falling = false
	running = false
	runningdb = false
	climbing = false
	locked = false
	LPlayer = nil
	RunAnimHandler = nil
	Humanoid.WalkSpeed = 10
end)

function updateBobbleEffect()
	local currentTime = tick()
	if Humanoid.MoveDirection.Magnitude > 0 then
		if not running then
			local bobbleX = math.cos(currentTime * 8) * .25
			local bobbleY = math.abs(math.sin(currentTime * 8)) * .25        

			local bobble = Vector3.new(bobbleX, bobbleY, 0)

			Humanoid.CameraOffset = Humanoid.CameraOffset:lerp(bobble, .5)
		else
			if not RunAnimHandler.IsPlaying and not jumping and not falling and not Dashing and not climbing then
				RunAnimHandler:Play()
			end
			local bobbleX = math.cos(currentTime * 12) * .26
			local bobbleY = math.abs(math.sin(currentTime * 12)) * .26        

			local bobble = Vector3.new(bobbleX, bobbleY, 0)

			Humanoid.CameraOffset = Humanoid.CameraOffset:lerp(bobble, 1)

		end
	else 
		if running and RunAnimHandler ~= nil and tick() - walkingtime >= 1 then
			canrough = false
			local StopRun = Humanoid.Animator:LoadAnimation(StopRunning)
			RunAnimHandler:Stop()
			Humanoid.WalkSpeed = 0
			Humanoid.JumpPower = 0
			StopRun:Play()
			StopRun.Stopped:Wait()
			Humanoid.JumpPower = 50
			if running then
				Humanoid.WalkSpeed = 20
			end
		elseif running and RunAnimHandler ~= nil and tick() - walkingtime < 1 then
			RunAnimHandler:Stop()
			Humanoid.CameraOffset = Humanoid.CameraOffset * .75
		end
	end
end


UserInputService.InputBegan:Connect(function(input,gameprocessed)
	if input and not gameprocessed then
		if input.KeyCode == Enum.KeyCode.LeftShift and not falling and not jumping and not Character.HumanoidRootPart:FindFirstChild("BodyVelocity") then
			if not running and not runningdb then
				walkingtime = tick()
				runningdb = true
				running =  true
				Humanoid.WalkSpeed = 20
				local RunLoad = Humanoid.Animator:LoadAnimation(RunningAnim)
				RunAnimHandler = RunLoad
				--if Humanoid.MoveDirection.Magnitude > 0 and not jumping and not falling then
				--	RunLoad:Play()
				--end
				task.wait(1)
				runningdb = false
			elseif running and not runningdb then
				walkingtime = tick()
				runningdb = true
				running = false
				Humanoid.WalkSpeed = 10
				if RunAnimHandler.IsPlaying then
					RunAnimHandler:Stop()
				end
				task.wait(1)
				runningdb = false
			end
		end
		if input.KeyCode == Enum.KeyCode.F then
			if not locked then
				if mouse.Target ~= nil then
					if mouse.Target.Parent:FindFirstChild('Humanoid') then
						local targethmn = mouse.Target.Parent:FindFirstChild('Humanoid') 
						if targethmn.Health > 0 then
							locked = true
							Humanoid.AutoRotate = false
							UserInputService.MouseIconEnabled = false
							LPlayer = mouse.Target.Parent
						end

					else
						Humanoid.AutoRotate = true
						UserInputService.MouseIconEnabled = true
						LPlayer.HumanoidRootPart:FindFirstChild('LockOnGui'):Destroy()
						Player.PlayerGui:FindFirstChild('EnemyHealth'):Destroy()
						locked = false
						LPlayer = nil
					end
				end
			end
		end
	end
end)

Humanoid.StateChanged:Connect(function(_oldState, newState)
	if newState == Enum.HumanoidStateType.Jumping then
		jumping = true 
		if running then
			if RunAnimHandler.IsPlaying  then
				RunAnimHandler:Stop()
			end
		end
	end
	if newState == Enum.HumanoidStateType.Freefall then
		falling = true
		jumping = false
		if running and RunAnimHandler.IsPlaying then
			RunAnimHandler:Stop()
		end
	end
	if newState == Enum.HumanoidStateType.Landed and _oldState == Enum.HumanoidStateType.Freefall then
		Humanoid.JumpPower = 0
		Humanoid.WalkSpeed = 6
		local FelledL = Humanoid.Animator:LoadAnimation(FelledAnim)
		FelledL:Play()
		FelledL.Stopped:Wait()
		falling = false
		if running then
			--RunAnimHandler:Play()
			Humanoid.WalkSpeed = 20
		else
			Humanoid.WalkSpeed = 10
		end
		task.wait(1)
		Humanoid.JumpPower = 50
	end
	if newState == Enum.HumanoidStateType.Climbing then
		climbing = true
		if running then
			runningdb = true
			running = false
			Humanoid.WalkSpeed = 10
			if RunAnimHandler.IsPlaying then
				RunAnimHandler:Stop()
			end
			task.wait(1)
			runningdb = false
		end
	end
	if _oldState == Enum.HumanoidStateType.Climbing then
		climbing = false
	end
end)

runService.RenderStepped:Connect(function()
	updateBobbleEffect()

	UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
	UserInputService.MouseIconEnabled = false

	if locked == true then
		Character.Humanoid.CameraOffset = Vector3.new(2, 0, 0)
		if LPlayer ~= nil then
			local hmn = LPlayer.Humanoid
			if hmn.Health > 0 then
				Camera.CFrame = CFrame.lookAt(Camera.CFrame.Position+Vector3.new(0,1.5,0.4),LPlayer.HumanoidRootPart.Position)
				Character.HumanoidRootPart.CFrame = CFrame.new(Character.HumanoidRootPart.Position, Vector3.new(LPlayer.HumanoidRootPart.Position.X,Character.HumanoidRootPart.Position.Y,LPlayer.HumanoidRootPart.Position.Z))
			else
				Humanoid.AutoRotate = true
				UserInputService.MouseIconEnabled = true
				locked = false
				LPlayer = nil

			end
		end
	else
		Character.Humanoid.CameraOffset = Vector3.new(0, 1.2, -1.2)
	end

	if not running and RunAnimHandler ~= nil then
		if RunAnimHandler.IsPlaying then
			RunAnimHandler:Stop()
		end
	end
end)

i would be grateful for any help

2 Likes

Don’t worry. I’m able to help you.

-- You must create an animation inside the script object named "Quickstop".
local canQuickStop = false
local minspeed = 24 -- Set this to the minimum running speed required for a stop animation to play.
local humanoid = script.Parent:WaitForChild("Humanoid", nil)
humanoid.FallingDown:Connect(function(active)
	if active then
		canQuickStop = false
		
		local speed = humanoid.Running:Wait()
		
		if speed > minspeed then
			canQuickStop = true
		end
	end
end)
humanoid.Jumping:Connect(function(active)
	if active then
		canQuickStop = false
	end
end)
humanoid.Running:Connect(function(speed)
	if speed == 0  and script.Parent.Humanoid.WalkSpeed > minspeed then
		if canQuickStop then
			canQuickStop = false
			local qsanim = script.Parent.Humanoid:LoadAnimation(script.Quickstop)
			qsanim:Play()
			qsanim:AdjustSpeed(math.random(1,1.5))
		--	game:GetService("HapticService"):SetMotor(Enum.UserInputType.Gamepad1, Enum.VibrationMotor.Large, .5)
			script.Parent.Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)
			script.Parent.Humanoid:SetStateEnabled(Enum.HumanoidStateType.Running, false)
			qsanim.Stopped:Connect(function()
				--game:GetService("HapticService"):SetMotor(Enum.UserInputType.Gamepad1, Enum.VibrationMotor.Large, 0)
				script.Parent.Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, true)
				script.Parent.Humanoid:SetStateEnabled(Enum.HumanoidStateType.Running, true)
			end)
			end
	elseif speed > minspeed then
		canQuickStop = true
	end
end)
2 Likes

ITS WORKS!!! thank you so much my lord i owe you my soul

1 Like

stop

2 Likes

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