Sprint/Walk system problem

  1. What do you want to achieve? Keep it simple and clear!
    So I want to create a simple OOP-based movement system with walk and sprint.

  2. What is the issue? Include screenshots / videos if possible!
    The sprint animation is not applied when I hold the left shift. It just speeds up the character’s walk speed. It changes if I jump and then hold the left shift but then we have the same problem again when switching from sprint animation to walk animation. It’s just overall buggy.

Here’s a video:
robloxapp-20231204-1603083.wmv (2.3 MB)

And here’s a screenshot of the system structure:
Skärmbild 2023-12-04 160658

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve searched a lot on the dev hub but couldn’t understand how to fix it.

Also, I don’t understand whether I should use the built-in Animator script or to create my own. I am short on time so I would like to avoid that.

Here’s the code(a module script )

local Movement = {}
Movement.__index = Movement

-- Speeds --
local walkSpeed = 14
local sprintSpeed = 20
local jumpPower = 7
local maxSprintTime = 5
------------
-- Variables --
local player = game:GetService("Players").LocalPlayer
local SetWalkSpeed = game.ReplicatedStorage.Events["Local-Server"].Movement:WaitForChild("SetWalkSpeed")

local walkAnim = Instance.new("Animation")
walkAnim.AnimationId = "rbxassetid://"..15533751433

local sprintAnim = Instance.new("Animation")
sprintAnim.AnimationId = "rbxassetid://"..15524882997


---------------
---------------
-- Services --
local CAS = game:GetService("ContextActionService")
local UIS = game:GetService("UserInputService")
--------------



local sprintBind
local characterAddedBind



function Movement:Disable()
	CAS:UnbindAction("sprintBind")
	characterAddedBind:Disconnect()
	
	setmetatable(self, nil)
	table.clear(self)
	table.freeze(self)
end


function Movement:Sprint(actionName, inputState, _inputObject)
	if inputState == Enum.UserInputState.Begin then
		-- fire evnt to increse walkspeed + animation
		
		local animator = player.Character.Humanoid:FindFirstChildOfClass("Animator")
		local track = animator:LoadAnimation(sprintAnim)
		track:Play()
		SetWalkSpeed:FireServer(sprintSpeed)
		track:Stop()
		player.Character.Animate.run.RunAnim.AnimationId = sprintAnim.AnimationId
		
		self.IsSprinting = true
	elseif inputState == Enum.UserInputState.End then
		-- fire evnt to decrese walkspeed + animation
		
		
		local animator = player.Character.Humanoid:FindFirstChildOfClass("Animator")
		local track = animator:LoadAnimation(walkAnim)
		track:Play()
		SetWalkSpeed:FireServer(walkSpeed)
		track:Stop()
		player.Character.Animate.run.RunAnim.AnimationId = walkAnim.AnimationId
		
		self.IsSprinting = false
	end
end



function Movement:UpdateChar()
	player.Character.Animate.run.RunAnim.AnimationId = "rbxassetid://"..sprintAnim
end



function Movement.New()
	local self = setmetatable({}, Movement)
	
	self.IsSprinting = false
	sprintBind = CAS:BindAction("sprintBind", function(x,y,z) self:Sprint(x,y,z) end, false, Enum.KeyCode.LeftShift)
	
	player.Character.Animate.run.RunAnim.AnimationId = walkAnim.AnimationId
	
	characterAddedBind = player.CharacterAdded:Connect(function() self:UpdateChar() end)
	
	
	return Movement
end


return Movement

I would appreciate any help :slight_smile: !

3 Likes

Try changing the animation priority to action. This makes the animation have higher priority than the default walking animation (which i presume have the ‘Movement’ priority).

image

If that doesn’t work, I’d suggest looking into HumanoidStateType.
When sprinting, the humanoid is not changing states which might cause the animation to not switch immediately after sprinting, and then when the player jumps the HumanoidStateType changes to jump and then running after landing. And now the new animation can play and therefore it works after jumping. If that’s the case, try quickly switching the state to another and then back to running for a hacky solution.

But changing the animation priority should work as well, and better.

Edit: Grammar

3 Likes

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