-
What do you want to achieve?
I want to use this tutorial so that I can make directional walking.
-
What is the issue?
The issue is that, the animation doesn’t play correctly. Sometimes, the animations don’t play at all. I have another script that edits the Humanoid’s walkspeed, and I think that’s causing the problem.
robloxapp-20231105-1327575.wmv (1.0 MB)
-
What solutions have you tried so far?
I tried disabling the movement script, but it did not work. I made sure that the animations are on priority movement.
--Animate Script
local RunService = game:GetService("RunService")
local Figure = script.Parent
local MyHumanoid = Figure:FindFirstChildWhichIsA("Humanoid")
local MyHRP = Figure:WaitForChild("HumanoidRootPart") :: BasePart
MyHumanoid:SetStateEnabled(Enum.HumanoidStateType.Ragdoll, false)
MyHumanoid:SetStateEnabled(Enum.HumanoidStateType.FallingDown, false)
local function getAnim(ID: string)
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://".. ID
return animation
end
local DirectionalMovement = {
["WalkFoward"] = MyHumanoid:LoadAnimation(getAnim(15279882595)),
["WalkRight"] = MyHumanoid:LoadAnimation(getAnim(15280017468)),
["WalkLeft"] = MyHumanoid:LoadAnimation(getAnim(15280030551))
}
for _, Animation: AnimationTrack in pairs(DirectionalMovement) do
Animation.Priority = Enum.AnimationPriority.Action4
Animation:Play(0, 0.01, 0)
end
local Idle = MyHumanoid:LoadAnimation(getAnim(15017430611))
Idle:AdjustSpeed(0)
Idle:Play()
RunService.RenderStepped:Connect(function(dt)
local MoveDirection = MyHRP.CFrame:VectorToObjectSpace(MyHRP.AssemblyLinearVelocity)
local Forward = math.abs(math.clamp(MoveDirection.Z/MyHumanoid.WalkSpeed, -1, 0.01))
local Backward = math.abs(math.clamp(MoveDirection.Z/MyHumanoid.WalkSpeed, 0.01, 1))
local Right = math.abs(math.clamp(MoveDirection.X/MyHumanoid.WalkSpeed, 0.01, 1))
local Left = math.abs(math.clamp(MoveDirection.X/MyHumanoid.WalkSpeed, -1, 0.01))
local SpeedUnit = (MoveDirection.Magnitude/MyHumanoid.WalkSpeed)
if MoveDirection.Z/MyHumanoid.WalkSpeed < 0.1 then
DirectionalMovement.WalkFoward:AdjustWeight(Forward)
DirectionalMovement.WalkRight:AdjustWeight(Right)
DirectionalMovement.WalkLeft:AdjustSpeed(Left)
for _, track: AnimationTrack in pairs(DirectionalMovement) do
track:AdjustWeight(SpeedUnit)
end
else
--Backwards
DirectionalMovement.WalkFoward:AdjustWeight(Backward)
DirectionalMovement.WalkRight:AdjustWeight(Left)
DirectionalMovement.WalkLeft:AdjustSpeed(Right)
for _, track: AnimationTrack in pairs(DirectionalMovement) do
track:AdjustWeight(SpeedUnit*-1)
end
end
end)
--Movement Script
local ActionService = game:GetService("ContextActionService")
local InputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Player = game.Players.LocalPlayer
local camera = workspace.CurrentCamera
local plrScripts = Player.PlayerScripts :: PlayerScripts
local plrModule : {} = require(plrScripts:WaitForChild("PlayerModule"))
local Controls = plrModule:GetControls() :: {}
local inputVel = Vector3.new()
local wishDir = Vector3.new()
local playerVel = Vector3.new()
local wishJump = false
local canJump = true
local SIDE_STRAFE_SPEED = 1
local SIDE_STRAFE_ACCEL = 100
local MAX_SPEED = 100 --320
local MOVE_SPEED = 25
local MAX_ACCEL = 10*MAX_SPEED
local AIR_FRICTION = 3
local FRICTION = 8
local RUN_DEACCEL = 5
local AIR_ACCEL = 2.5
local JUMP_BUFFER = .1
local HOLD_JUMP_TO_BHOP = true
local K_CURVATURE = 2.0
local K_DEADZONE = 0.15
local humanoid: Humanoid = nil
local hrp: BasePart = nil
local function keyHandler(key: InputObject)
if key.KeyCode == Enum.KeyCode.W then
inputVel += Vector3.new(0, 0, -.9)
end
if key.KeyCode == Enum.KeyCode.A then
inputVel += Vector3.new(-1, 0, 0)
end
if key.KeyCode == Enum.KeyCode.S then
inputVel += Vector3.new(0, 0, .9)
end
if key.KeyCode == Enum.KeyCode.D then
inputVel += Vector3.new(1, 0, 0)
end
end
local function FCurve(X: number): number
return (math.exp(K_CURVATURE * X) - 1) / (math.exp(K_CURVATURE) - 1)
end
local function FDeadzone(X: number): number
return FCurve((X - K_DEADZONE) / (1 - K_DEADZONE))
end
local function ThumbstickCurve(X: number): number
return math.sign(X) * math.clamp(FDeadzone(math.abs(X)), 0, 1)
end
local function accelerate(wishSpeed: number, accel: number)
local currentSpeed = playerVel:Dot(wishDir)
local addSpeed = wishSpeed - currentSpeed
if addSpeed <= 0 then
return
end
local accelSpeed = accel * RunService.Heartbeat:Wait() * wishSpeed
accelSpeed = accelSpeed > addSpeed and addSpeed or accelSpeed
local x = playerVel.X + accelSpeed * wishDir.X
local z = playerVel.Z + accelSpeed * wishDir.Z
playerVel = Vector3.new(x, 0, z)
end
local function applyFriction(dt: number)
local inAir = humanoid.FloorMaterial == Enum.Material.Air
local vec = Vector3.new(playerVel.X, 0, playerVel.Z)
local speed = vec.Magnitude
local newFriction = inAir and AIR_FRICTION or FRICTION
local control = speed < RUN_DEACCEL and RUN_DEACCEL or speed
local drop = control * newFriction * RunService.Heartbeat:Wait() * dt
local newSpeed = speed - drop
newSpeed = newSpeed < 0 and 0 or newSpeed
newSpeed = speed>0 and newSpeed/speed or newSpeed
local x = playerVel.X*newSpeed
local z = playerVel.Z*newSpeed
playerVel = Vector3.new(x, 0, z)
end
local function ground_vel(relativeToCamera: boolean)
if not wishJump and canJump then
applyFriction(1)
else
applyFriction(0)
end
local _WishDir = relativeToCamera and Vector3.new(wishDir.X, 0, wishDir.Z) or inputVel
local wishSpeed = _WishDir.Magnitude*MOVE_SPEED
accelerate(wishSpeed, RUN_DEACCEL)
playerVel = Vector3.new(playerVel.X, 0, playerVel.Z)
if humanoid and wishJump and canJump then
humanoid.Jump = true
wishJump = false
canJump = false
task.spawn(function()
local Begin = tick()
while tick() - Begin < JUMP_BUFFER do
RunService.Heartbeat:Wait()
end
canJump = true
end)
end
end
local function air_vel(relativeToCamera: boolean)
if inputVel.Z ~= 0 then
applyFriction(1)
end
local _WishDirection = wishDir ~= Vector3.zero and wishDir.Unit or wishDir
_WishDirection = relativeToCamera and Vector3.new(_WishDirection.X, 0, wishDir.Z) or inputVel
local wishSpeed = _WishDirection.Magnitude*MOVE_SPEED
local accel: number = AIR_ACCEL
if inputVel.Z == 0 and inputVel.X ~= 0 then
if wishSpeed > SIDE_STRAFE_SPEED then
wishSpeed = SIDE_STRAFE_SPEED
end; accel = SIDE_STRAFE_ACCEL
end
accelerate(wishSpeed, accel)
end
local function queueJump()
if not humanoid then
return
end
local IsJumping = if (Controls.activeController and Controls.activeController.enabled and Controls.humanoid)
then (if (Controls.activeController:GetIsJumping() or (Controls.touchJumpController and Controls.touchJumpController:GetIsJumping()))
then true
else false)
else false
if not humanoid:GetStateEnabled(Enum.HumanoidStateType.Jumping) and IsJumping then
IsJumping = false
end
if HOLD_JUMP_TO_BHOP then
wishJump = IsJumping
else
if IsJumping and not wishJump then
wishJump = true
if Controls.activeController then
Controls.activeController.isJumping = false
elseif Controls.touchJumpController then
Controls.touchJumpController.isJumping = false
end
end
if not IsJumping then
wishJump = false
end
end
--wishJump = true
end
local function characterAdded(char: Model)
humanoid = char:WaitForChild("Humanoid")
hrp = char:WaitForChild("HumanoidRootPart")
end
local function onStep(dt: number)
local cameraCframe = camera.CFrame
wishDir = cameraCframe:VectorToWorldSpace(inputVel)
wishDir = Vector3.new(wishDir.X, 0, wishDir.Z)
accelerate(wishDir.Magnitude*MOVE_SPEED, RUN_DEACCEL)
playerVel = Vector3.new(playerVel.X, 0, playerVel.Z)
end
Controls.moveFunction = function(controller: Player, walkDirection: Vector3, RelativeToCamera: boolean)
RelativeToCamera = Controls.activeController and Controls.activeController:IsMoveVectorCameraRelative() or RelativeToCamera
if controller == Player then
local MoveVector: Vector3 = Controls:GetMoveVector()
MoveVector = Vector3.new(
ThumbstickCurve(MoveVector.X),
ThumbstickCurve(MoveVector.Y),
ThumbstickCurve(MoveVector.Z)
)
inputVel = MoveVector
if humanoid then
local grounded = humanoid.FloorMaterial ~= Enum.Material.Air
if grounded then
ground_vel(RelativeToCamera)
else
air_vel(RelativeToCamera)
end
local newDir = playerVel ~= Vector3.zero and playerVel.Unit or Vector3.zero
local newSpeed = playerVel.Magnitude
newSpeed = math.clamp(newSpeed, 0, MAX_SPEED)
humanoid.WalkSpeed = newSpeed
humanoid:Move(newDir, false)
end
end
end
Player.CharacterAdded:Connect(characterAdded)
RunService.RenderStepped:Connect(onStep)
InputService.JumpRequest:Connect(queueJump)