#ANIMATION WONT STOP

127 messages · Page 1 of 1 (latest)

weak tartan
#

basc, i made a animation and to start it i press a button and the animation is an loop but when i press the button again it doesn't stop the animation. how do i make it stop?

the code is
local click = script.Parent.MouseButton1Click
local players = game:GetService("Players")
local char = players.LocalPlayer.Character

if not char then
players.LocalPlayer.CharacterAdded:Wait()
end

local hum = char:WaitForChild("Humanoid")

local animation = Instance.new("Animation")

animation.AnimationId = "rbxassetid://" -- your id

local animationtrack = hum:LoadAnimation(animation)

function playAnimation()
animationtrack:Play()
end

click:Connect(playAnimation)

brazen ermine
#

animationTrack:Stop()

weak tartan
#

i tried that

#

when i pressed the button again it doesnt stop

brazen ermine
#

what did your code look like when you did that?

weak tartan
brazen ermine
#

yeah that wont work since it runs once before you even press the button

knotty kindleBOT
#

studio** You are now Level 2! **studio

weak tartan
#

.

brazen ermine
#

what you need to do is have a variable that toggles between true and false

weak tartan
#

.

#

um idk how to do that

brazen ermine
#
local variable = false

function OnClick()
  if variable == false then
      animationtrack:Play()
  else
      animationtrack:Stop
  end
  variable = not variable
end
#

the first time the variable will be false so it will play the animation. Then the variable sets to the opposite of itself (true)

weak tartan
#

it didnt work

brazen ermine
#

😬

#

you need to change it to fit your code

weak tartan
#

ohh

#

like this?

brazen ermine
#

the parentheses is missing from animationTrack:Stop()

weak tartan
#

ohh

shell brook
# weak tartan
local Animator = hum:FindFirstChildOfClass("Animator")

local function stopAnimation(Animator, animation)
    for index, anim in pairs(Animator:GetPlayingAnimationTracks()) do
        if anim.Name == animation then
            anim:Stop()
        end
    end
end

-- call it by: stopAnimation(Animator, "[name of the animation you want to stop]")```
weak tartan
#

.

knotty kindleBOT
#

studio** You are now Level 2! **studio

shell brook
#

with this, you can stop animations in also different scripts

brazen ermine
#

that's if multiple animations are playing tho

#

in this case just one

shell brook
#

yes, but there are cases you want to stop animations and it is loaded in a different script

weak tartan
#

yea

shell brook
#

it's just easier to use this

#

I use it in everything, so I don't have to struggle with organizating animation tracks variables

brazen ermine
#

yeah that makes sense

weak tartan
#

i think its my code

#

cuz nun of what yall both said worked

shell brook
#

show what you did

#

in my way

weak tartan
shell brook
#

no, you weren't supposed to delete your scritp 😭

weak tartan
#

.

#

I DIDINT

shell brook
#

that's a function. you were supposed to call it inside the else statement

weak tartan
#

i put a line between ur crip and mines

shell brook
#

where is the OnClick function?

weak tartan
#

...

shell brook
#

use ctrl + z to go to past

weak tartan
shell brook
#

you call that stopAnimation function inside the if else statement

#

wait, I just noticed what you're doing

weak tartan
#

what am i doing?

shell brook
#

okay so you want the animation to stop when the player clicks a 2nd time, right?

weak tartan
#

yes

#

when i click it the animation plays

shell brook
#

how do you think we are gonna know if it's the 2nd click or not?

weak tartan
#

when i click again it stops

shell brook
#

for that, make a variable. name it wtv and make it's value = 0

#

when the player clicks, the variable is gonna add 1 to it's value (which is 0 before adding 1)

#

if the value of the variable equals one, then it'll play

#

else

#

it'll call the stopAnimation function

#

@weak tartan any struggles at doing this without an example?

weak tartan
#

ummmmmmmmmmmmmmmmmmmmmmmmmmm

#

yea

shell brook
#

okay lemem show u

weak tartan
#

ok ty

shell brook
#
local number = 0

click.MouseButton1Click:Connect(function()
    if number == 1 then
        number = 0
        playAnimation()
    else
        number = 1
        stopAnimation(Animator, "[name of the anim]")
    end
end)```
knotty kindleBOT
#

studio** You are now Level 6! **studio

shell brook
#

also, don't just copy it, make sure you understood how it works

weak tartan
#

ok so

shell brook
#

and also put the stopAnimation function under/above the playAnimation function

brazen ermine
#

why an integer value?

shell brook
#

it's a way to know if it's the 2nd or 1st

brazen ermine
#

but i think im just nitpicking

weak tartan
#

local number = 1

click.MouseButton1Click:Connect(function()
if number == 0 then
number = 1
playAnimation()
else
number = 0
animationtrack:Stop(umhey)
end
end)

#

?

shell brook
#

but yeah

shell brook
#

@weak tartan show me your script

weak tartan
#

?

#

anything wrong

shell brook
#

lemme see

shell brook
weak tartan
#

.

shell brook
#

remove the old click:Connect. also, make click = script.Parent, not the event itself

#

and make a variable under hum called "Animator", which equals hum:FindFirstChildOfClass("Animator")

weak tartan
shell brook
#

wyd

weak tartan
#

.

#

im so confused

shell brook
#

change the value of the click variable, not put the value anywhere

weak tartan
#

ohhh

#

so change the number to = 1?

shell brook
#

no, I said click variable

#

one second

weak tartan
#

sorry for me being dumb lol

knotty kindleBOT
#

studio** You are now Level 3! **studio

shell brook
#
local click = script.Parent
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:FindFirstChildOfClass("Animator") or Humanoid:WaitForChild("Animator")

local animation = Instance.new("Animation")

animation.Name = "HELLO"
animation.AnimationId = "rbxassetid://13737333639"

local trackAnim = Humanoid:LoadAnimation(animation)

local function stopAnimation(Animator, animation)
    for i, v in pairs(Animator:GetPlayingAnimationTracks()) do
        if v.Name == animation then
            v:Stop()
        end
    end
end

local number = 0

click.MouseButton1Click:Connect(function()
    if number == 1 then
      number = 0
      trackAnim:Play()
    else
      number = 1
       stopAnimation(Animator, "HELLO")
    end
end)```
#

@weak tartan watch YT scripting guides

#

it's gonna help you a lot at learning it

#

you seemed to be confused on things you should've know for what you're trying to do

#

personally, I like TheDevKing's beginner playlsit

weak tartan
shell brook
#

this is Lua

weak tartan
shell brook
#

I mean, Luau, which is Lua adapted to Roblox

weak tartan
#

like this?

shell brook
weak tartan
shell brook
#

tell me if there's anything you didn't understand on it

#

however I need to sleep in a bit

weak tartan
#

TYSMMMMMMMMMM

#

!

shell brook
#

no problem, but I know you didn't understand much of it

weak tartan
#

ima start learning rght now

shell brook
#

first watch all the beginner guide before trying to practice your own ideas

#

after that, start watching the advanced guide and always keep using DevForum website and practicing your ideas

weak tartan
#

i did that by using block coding and turning it into java script

shell brook
#

anyways good night