I’m making an angel rpg and have been trying to learn scripting so I can manage to do some things by myself without hiring a team to do everything. So I looked up a tutorial and used a fly script from Suphi Kaner that allowed me to understand the uses behind certain functions. The script works great but I wanted to make my own small adjustments.
Here is my issue
I added my own flying animations, and I made them play based on if they’re Idle or Moving. This is the code that makes that work
--Animations--
local FlyForward = script:WaitForChild("FlyForward")
local animationTrack = character.Humanoid.Animator:LoadAnimation(FlyForward)
animationTrack.Priority = Enum.AnimationPriority.Movement
local FlyIdle = script:WaitForChild("FlyIdle")
local flyIdleAnimTrack = character.Humanoid.Animator:LoadAnimation(FlyIdle)
flyIdleAnimTrack.Priority = Enum.AnimationPriority.Idle
local function FlyAction(actionName, inputState, inputObject)
if inputState ~= Enum.UserInputState.Begin then return Enum.ContextActionResult.Pass end
if connection == true then return Enum.ContextActionResult.Pass end
if connection == nil then
connection = true
if character.Humanoid.FloorMaterial ~= Enum.Material.Air then -- if character is standing on the floor when flying, will make character jump before flight
character.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
task.wait(0.1)
end
--Flight Mode--
vectorForce.Enabled = true
alignOrientation.CFrame = primaryPart.CFrame
alignOrientation.Enabled = true
flyIdleAnimTrack:Play() --the animation it starts with
character.Humanoid:ChangeState(Enum.HumanoidStateType.Physics) --this makes it so our humanoid is not in control of the fly physics
connection = runService.Heartbeat:Connect(function(deltaTime)
vectorForce.Force = gravityVector * primaryPart.AssemblyMass
local moveVector = Vector3.new(character.Humanoid.MoveDirection.X, yAxis, character.Humanoid.MoveDirection.Z)
if moveVector.Magnitude > 0 then
animationTrack:Play() --switches to flying animation when moving
moveVector = moveVector.Unit
vectorForce.Force += moveVector * force * primaryPart.AssemblyMass
if math.abs(moveVector.Y) == 1 then
alignOrientation.CFrame = CFrame.lookAt(Vector3.new(0, 0, 0), moveVector -primaryPart.CFrame.LookVector) * CFrame.fromOrientation(-math.pi / 2, 0, 0)
else
alignOrientation.CFrame = CFrame.lookAt(Vector3.new(0, 0, 0), moveVector) --* CFrame.fromOrientation(-math.pi / 2, 0, 0) <-- Makes character flip forward when flying forward
end
else animationTrack:Stop() --switches back to idle animation (our default)
end
Now it works as intended- when I stop moving, the flying Idle plays, when I move the flying forward animation plays. But the issue is- the flying forward animation DOESN’T play. It freezes mid animation when I fly around.
Example
I know the animation isn’t the problem, because when I replace the default animation (FlyIdleTrack) to the flying forward animation, the animation plays fine. So I’m guessing its the way I input the code that makes it look laggy and freezes. I tried alternatives, like placing the the animations in different parts of the script, or switching the animations places, but it doesn’t work as intended. I’m stumped on how to fix this.
I also tried asking for help in Suphi’s discord, (a server where programmers help each other and has more tutorials) and even asked Suphi himself but I was just ignored.