-
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. -
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:
-
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 !