How can i add animation

Hello, I made a flying script, but I want to add an animation in this script. I’ve never done this so don’t know how to add. I try in many ways but nothing works. Can anybody help?

LOCALSCRIPT

repeat wait() until game.Players.LocalPlayer.Character

local player = game.Players.LocalPlayer
local char = player.Character
local hum = char:WaitForChild("Humanoid")
local tor = char:WaitForChild("LowerTorso")
local mouse = player:GetMouse()
local de = false

mouse.KeyDown:Connect(function(key)
	if key == "f" then
		if de == false  then
			local anim = Instance.new("Animation")
			anim.AnimationId = "rbxassetid://4516044669"
			local play = hum:LoadAnimation(anim)
			play:Play()
			de = true
			local bodyvel = Instance.new("BodyVelocity",tor)
			bodyvel.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
			
			while de == true do
				bodyvel.Velocity = mouse.Hit.LookVector * 50
				wait()
			end
		end
		if de == true then
			de = false
			tor:FindFirstChildOfClass("BodyVelocity"):Destroy()
		end
	end
end)

1 Like

Are you using user input service? And where is the script located?

1 Like

This is the entire script. Script is in StarterPack.

2 Likes
local userInput = game:GetService("UserInputService")
local players = game:GetService("Players")
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://2510199791"
animation.Parent = script
local track = animator:LoadAnimation(animation)
track.Looped = true
local torso = character:WaitForChild("LowerTorso")
local mouse = player:GetMouse()
local toggle = false

userInput.InputBegan:Connect(function(key, processed)
	if processed then return end
	
	if key.KeyCode == Enum.KeyCode.F then
		toggle = not toggle
		if toggle then
			track:Play()
			local bodyVelocity = Instance.new("BodyVelocity")
			bodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
			bodyVelocity.Parent = torso
			while true do
				task.wait()
				if toggle then
					bodyVelocity.Velocity = mouse.Hit.LookVector * 50
				else
					bodyVelocity:Destroy()
					break
				end
			end
		end
	end
end)

I tested using the “RThro Swim” animation and it worked.

https://www.roblox.com/catalog/2510240941/Rthro-Swim

1 Like

Yes, this script WORKS , I only add animation disabling via track:Stop (), but where can I find a flying animation that would work too. I can’t find any in the roblox library.