Hi! So I’m trying to stop an Animationtrack but for some reason I can’t.
Code:
local player = game.Players.LocalPlayer
local toggle = false
local animation = script:WaitForChild("FlyAnimation")
local runService = game:GetService("RunService")
player.Chatted:Connect(function(msg)
msg = string.gsub(msg, "%c", "")
local prefix = ":fly"
local splits = msg:split(" ")
if splits[1]:lower() == prefix and player:WaitForChild("leaderstats").Rank.Value >= 7 then
--Make him fly
if toggle == false then
toggle = true
local animationTrack = player.Character.Humanoid:LoadAnimation(animation)
local BV = Instance.new("BodyVelocity", player.Character.HumanoidRootPart)
BV.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
animationTrack.Looped = true
animationTrack.Name = "FlyAnimation"
animationTrack:Play()
runService.RenderStepped:Connect(function()
BV.Velocity = player:GetMouse().Hit.LookVector * 30
player.Character.HumanoidRootPart.CFrame = CFrame.new(player.Character.HumanoidRootPart.Position, player.Character.HumanoidRootPart.Position + player:GetMouse().Hit.LookVector)
end)
elseif toggle == true then
--Stop the animation
--Even if I made the animationTrack variable global and did animationTrack:Stop(), here, it would still not completely stop, somehow, I've posted a vid about it below
end
end
end)
Video:
My humanoid is in a weird state, after stopping the animation, for some reason.
Thank you!
I could just reset my character and put him back to the position he was at, earlier, that would be the easiest thing, probably, but the problem is, that my camera manipulations would start playing again, as the camera manipulations script is inside of StarterCharacterScripts and the Character would get added.
Pretty sure the issue is that the BodyVelocity is still running. Additionally, there’s a lot of variables in your script that you’ll likely want to reference later, including both the animationTrack and the BodyVelocity. You should also be using BindToRenderStep()
and UnbindFromRenderStep()
so that you can call back to the connection you’re creating later on. Those two methods take a name
parameter, which makes them–at least for something like this–better than just connecting a function to RenderStepped. Try this:
-- Initialize some stuff
local player = game.Players.LocalPlayer
local toggle = false
local runService = game:GetService("RunService")
local animation = script:WaitForChild("FlyAnimation")
local animationTrack = player.Character.Humanoid:LoadAnimation(animation)
local BV
-- Method
player.Chatted:Connect(function(msg)
msg = string.gsub(msg, "%c", "")
local prefix = ":fly"
local splits = msg:split(" ")
if splits[1]:lower() == prefix and player:WaitForChild("leaderstats").Rank.Value >= 7 then
--Make him fly
if toggle == false then
toggle = true
BV = Instance.new("BodyVelocity", player.Character.HumanoidRootPart)
BV.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
animationTrack.Looped = true
animationTrack.Name = "FlyAnimation"
animationTrack:Play()
local function UpdateFly()
BV.Velocity = player:GetMouse().Hit.LookVector * 30
player.Character.HumanoidRootPart.CFrame = CFrame.new(player.Character.HumanoidRootPart.Position, player.Character.HumanoidRootPart.Position + player:GetMouse().Hit.LookVector)
end)
-- Bind the UpdateFly() method
runService:BindToRenderStep("UpdateFly", Enum.RenderPriority.Camera.Value - 1, UpdateFly)
-- if already flying, disable fly
elseif toggle == true then
-- toggle off
toggle = false
-- Unbind!
runService:UnbindFromRenderStep("UpdateFly")
--Stop the animation
animationTrack:Stop()
-- destroy the BodyVelocity
if BV ~= nil then
BV:Destroy()
end
end
end
end)
I tested the flying itself (without the animation) and it works just fine here:
Let me know if it works for you!
1 Like