Run code help need

Hello everyone, i make sprint function and uhm i have problem

When i press Shift (when standing), my character running (using animation and +speed boost), yeah when i not pressing Shift character don’t starting animation run and not speed boost

I need that when the player pinches the shift in PLACE, he does not play the running animation, and when he starts walking, then you can play the animation, but while running, if the player stops, it is necessary that his speed and animation reset

here code

local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Char = Player.Character
local hum = Char:FindFirstChildOfClass("Humanoid") 
local RepStorage = game:GetService("ReplicatedStorage")

local IsRunning = false
local isJumping = false

local walkSpeed = hum.WalkSpeed -- Скорость ходьбы по умолчанию
local jumppower = hum.JumpPower
local runSpeed = 24 -- Скорость бега
local jumpPower = 0
local runAnim = RepStorage.Animations.RunAnim
local runAnimationTrack = hum:LoadAnimation(runAnim)

hum.UseJumpPower = true





local function ShiftPressed()
	if hum:GetState() == Enum.HumanoidStateType.Jumping or hum:GetState() == Enum.HumanoidStateType.Freefall then
		isJumping = true
	end
	if IsRunning and not isJumping then
		hum.WalkSpeed = walkSpeed
		runAnimationTrack:Stop()
		IsRunning = false
	elseif not isJumping then
		hum.WalkSpeed = runSpeed
		runAnimationTrack:Play()
		IsRunning = true
		
		hum.Running:Connect(function(speed)
			if speed <= 0 then
				runAnimationTrack:Stop()
				hum.WalkSpeed = walkSpeed
				IsRunning = false
			end	
		end)
	end
end

local function handleShiftReleased()
	isJumping = false
	if IsRunning then
		hum.WalkSpeed = walkSpeed
		runAnimationTrack:Stop()
		IsRunning = false
	end
end




UserInputService.InputBegan:Connect(function(input, _gameProcessed)
	if input.KeyCode == Enum.KeyCode.LeftShift and not _gameProcessed then
		ShiftPressed()
		hum.JumpPower = jumpPower
	end
end)

UserInputService.InputEnded:Connect(function(input, _gameProcessed)
	if input.KeyCode == Enum.KeyCode.LeftShift and not _gameProcessed then
		handleShiftReleased()
		hum.JumpPower = jumppower
	end
end)

You can achieve this by changing the walking animation in the characters humanoid instead of animating the player.

I have a built-in walking animation, in the script running animation with Shift pressed, I need the player not to play the animation when standing still

Looks like it’s not the built in animation what you could do is this

player.Character.Animate.walk.WalkAnim.AnimationId = "rbxassetid://ID"
player.Character.Animate.run.RunAnim.AnimationId = "rbxassetid://ID"

Just make sure that player is not running.

To do that, you can check MoveDirection of the humanoid’s character if it equals or more than zero.
If it equals zero then player is standing, but if it’s more than zero then player is moving.
You may could use running event but its not that really perfect but yet it is another answer.

To check MoveDirection value you need to read his magnitude and check if it equals or more than zero.

Example:

-- variable "hum" is already stated in your script

if hum.MoveDirection.Magnitude == 0 then
	print("No Motion") -- prints string if player is not walking
elseif hum.MoveDirection.Magnitude > 0 then
	print("Motion") -- prints string if player is walking
end

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