I want the movement to not stop when I walk in the same direction but also keep the script from spamming because that’s what the bit that’s breaking it does (I think).
If I go the same direction twice it breaks.
https://gyazo.com/cff7a6c172ab952e8313ddf221360f3f
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local StarterPlayer = game:GetService("StarterPlayer")
local PlayersService = game:GetService("Players")
local WorkspaceSevice = game:GetService("Workspace")
local LocalPlayer = PlayersService.LocalPlayer
local CurrentCam = WorkspaceSevice.CurrentCamera
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local TagetMoveVelocity = Vector3.new()
local MoveVelocity = Vector3.new()
local MoveAcceleration = 20
local Animator = Character:WaitForChild("Humanoid").Animator
local Animations = {}
local Animation = nil
LocalPlayer.CharacterAdded:Connect(function(CharacterVar)
Character = CharacterVar
end)
local Module = {
KeyBindings = {
Forward = {Key = Enum.KeyCode.W, Direction = Enum.NormalId.Front},
Backward = {Key = Enum.KeyCode.S, Direction = Enum.NormalId.Back},
Left = {Key = Enum.KeyCode.A, Direction = Enum.NormalId.Left},
Right = {Key = Enum.KeyCode.D, Direction = Enum.NormalId.Right}
},
WalkSpeeds = {
Accel = StarterPlayer:GetAttribute("Acceleration"),
Forward = StarterPlayer:GetAttribute("Front"),
Backward = StarterPlayer:GetAttribute("Back"),
Left = StarterPlayer:GetAttribute("Left"),
Right = StarterPlayer:GetAttribute("Right")
},
Animations = {
Value = true,
Run = script:WaitForChild("Animations").Run,
Forward = script:WaitForChild("Animations").Front,
Backward = script:WaitForChild("Animations").Back,
Left = script:WaitForChild("Animations").Left,
Right = script:WaitForChild("Animations").Right
}
}
local function LerpFunc(One, Two, Three)
return One + ((Two - One) * Three)
end
local function GetWalkDirection()
local WalkDirection = Vector3.new()
for KeyBindPressed, Bind in pairs(Module.KeyBindings) do
if UserInputService:IsKeyDown(Bind.Key) then
WalkDirection += Vector3.FromNormalId(Bind.Direction)
end
end
if WalkDirection.Magnitude > 0 then
WalkDirection = WalkDirection.Unit
end
return WalkDirection
end
local function WorldSpaceFunc()
local WalkDirection = CurrentCam.CFrame:VectorToWorldSpace(GetWalkDirection())
WalkDirection *= Vector3.new(1, 0, 1)
if WalkDirection.Magnitude > 0 then
WalkDirection = WalkDirection.Unit
end
return WalkDirection
end
local function PlayTheAnimationFunc(Animation)
local Anim = Animations[Animation.Name]
if not Anim then
Anim = Animator:LoadAnimation(Animation)
Animations[Animation.Name] = Anim
end
if Anim.IsPlaying then
return Anim
end
Anim:Play()
Anim:GetMarkerReachedSignal("End"):Connect(function()
Anim:Stop()
end)
return Anim, Animation
end
local function StopTheAnimationsFunc()
for Index, Animat in pairs(Animations) do
if Animat.IsPlaying then
print(Animat.Name)
Animat:Stop()
end
end
end
local function GetTheMoveDirFunc()
local RootPart = Character:WaitForChild("Humanoid").RootPart
local Ford = RootPart.CFrame.LookVector
local Right = RootPart.CFrame.RightVector
local MoveDir = Ford * Character:WaitForChild("Humanoid").MoveDirection.Z + Right * Character:WaitForChild("Humanoid").MoveDirection.X
return MoveDir.Unit
end
local Anim = nil
local function UpdateMovementFunc(DeltaTime)
local MoveDirec = GetTheMoveDirFunc()
local Humanoid = Character:FindFirstChildOfClass("Humanoid")
if Humanoid then
local MoveDirection = WorldSpaceFunc()
TagetMoveVelocity = MoveDirection
MoveVelocity = LerpFunc(MoveVelocity, TagetMoveVelocity, math.clamp(DeltaTime * MoveAcceleration, 0, 1))
Humanoid:Move(MoveVelocity)
if Module.WalkSpeeds.Accel then
for KeyBindPressed, Bind in pairs(Module.KeyBindings) do
if UserInputService:IsKeyDown(Bind.Key) then
local Info = TweenInfo.new(0.6, Enum.EasingStyle.Quart)
local Tween = TweenService:Create(Humanoid, Info, {WalkSpeed = Module.WalkSpeeds[KeyBindPressed]}):Play()
end
end
end
if Module.Animations.Value then
if MoveDirec.Magnitude > 0 then
local For = MoveDirec:Dot(Vector3.new(0, 0, -1))
local Bac = MoveDirec:Dot(Vector3.new(0, 0, 1))
local Lef = MoveDirec:Dot(Vector3.new(-1, 0, 0))
local Rig = MoveDirec:Dot(Vector3.new(1, 0, 0))
local NewAnim = nil
if For > 0.5 then
NewAnim = Module.Animations.Forward
elseif Bac > 0.5 then
NewAnim = Module.Animations.Backward
elseif Lef > 0.5 then
NewAnim = Module.Animations.Left
elseif Rig > 0.5 then
NewAnim = Module.Animations.Right
end
if NewAnim and NewAnim ~= Animation then
print(NewAnim.Name)
StopTheAnimationsFunc()
Anim = PlayTheAnimationFunc(NewAnim)
Anim.Priority = Enum.AnimationPriority.Movement
Animation = NewAnim
end
else
StopTheAnimationsFunc()
Anim = nil
end
end
end
end
RunService.RenderStepped:Connect(UpdateMovementFunc)