I know there are already other posts about shift to run. But hear me out, I want to set a custom running animation and a custom walking animation. The running/walking animation will depend on the user’s weapon. Right now I only scripted a custom running script but it doesn’t change depending on the tool yet. I wanna know two things, is it fine my approach on a custom running animation and how should I set the different walking animations and running animations depending on the tool that is active. Keep in mind I wanna make the transition between animations very smooth. Thank you !
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local runSpeed = 30
local walkSpeed = 16
local runKey = Enum.KeyCode.LeftControl
local animationsFolder = ReplicatedStorage:WaitForChild("Animations"):WaitForChild("Fist")
local runAnimation = animationsFolder:WaitForChild("Run")
local Data = ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("Data")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:FindFirstChildOfClass("Humanoid")
local runAnimationTrack = humanoid:LoadAnimation(runAnimation)
runAnimationTrack.Priority = Enum.AnimationPriority.Action4
local wantsToRun = false
local lockRunningState = false
local isMoving = false
local function toggleRunning()
if character:GetAttribute("IsStunned") or character:GetAttribute("IsPunching") or character:GetAttribute("IsBlocking") then return end
wantsToRun = not wantsToRun
if isMoving then
humanoid.WalkSpeed = wantsToRun and runSpeed or walkSpeed
character:SetAttribute("IsRunning", wantsToRun)
if not wantsToRun then
runAnimationTrack:Stop()
else
runAnimationTrack:Play()
end
end
end
local function monitorMovementAndState()
local function checkAndPlayRunAnimation()
if character:GetAttribute("IsPunching") then
if runAnimationTrack.IsPlaying then
runAnimationTrack:Stop()
end
return
end
isMoving = humanoid.MoveDirection.Magnitude > 0
if wantsToRun and isMoving and humanoid.FloorMaterial ~= Enum.Material.Air then
if not runAnimationTrack.IsPlaying then
runAnimationTrack:Play()
character:SetAttribute("IsRunning", true)
end
elseif not isMoving or not wantsToRun then
if runAnimationTrack.IsPlaying then
runAnimationTrack:Stop()
character:SetAttribute("IsRunning", false)
end
end
if wantsToRun and isMoving then
humanoid.WalkSpeed = runSpeed
else
humanoid.WalkSpeed = walkSpeed
end
end
humanoid.Running:Connect(function(speed)
checkAndPlayRunAnimation()
end)
humanoid.StateChanged:Connect(function(oldState, newState)
if newState == Enum.HumanoidStateType.Landed then
checkAndPlayRunAnimation()
end
end)
end
local function init()
character = player.Character or player.CharacterAdded:Wait()
humanoid = character:FindFirstChildOfClass("Humanoid")
runAnimationTrack = humanoid:LoadAnimation(runAnimation)
character:SetAttribute("IsRunning", false)
character:SetAttribute("IsPunching", false)
monitorMovementAndState()
character:GetAttributeChangedSignal("IsPunching"):Connect(function()
if character:GetAttribute("IsPunching") then
runAnimationTrack:Stop()
humanoid.WalkSpeed = walkSpeed
character:SetAttribute("IsRunning", false)
end
end)
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then return end
if input.KeyCode == runKey then
toggleRunning()
end
end)
end
player.CharacterAdded:Connect(function(char)
character = char
init()
end)
init()
Data.OnClientEvent:Connect(function(data)
if data.action == "updateRunningState" then
character:SetAttribute("IsRunning", false)
lockRunningState = false
elseif data.action == "StopRunning" then
wantsToRun = false
humanoid.WalkSpeed = walkSpeed
runAnimationTrack:Stop()
character:SetAttribute("IsRunning", false)
end
end)
character:GetAttributeChangedSignal("IsPunching"):Connect(function()
if character:GetAttribute("IsPunching") then
wantsToRun = false
humanoid.WalkSpeed = walkSpeed
runAnimationTrack:Stop()
character:SetAttribute("IsRunning", false)
end
end)```