Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why cant I get this button to stop the animation?

I have this script right here:

--// Sit Animation
 
local player = game.Players.LocalPlayer
repeat wait() until player.Character.Humanoid
local humanoid = player.Character.Humanoid
local mouse = player:GetMouse()

local Sitanim = Instance.new("Animation")
Sitanim.AnimationId = "http://www.roblox.com/asset/?id=2519769373" -- id of animation
isAnim = false

script.Parent.MouseButton1Click:connect(function()
	if isAnim == false then 
		local playSitAnim = humanoid:LoadAnimation(Sitanim)
		playSitAnim:Play()
		isAnim = true
	end
	if isAnim == true then
		local playSitAnim = humanoid:LoadAnimation(Sitanim)
		playSitAnim:Stop()
		isAnim = false
	end
end)

I am trying to set "isAnim" to true or false to be able to "turn on" the animation and "turn it off" so I will be able to stop the animation when I want. But this doesnt seems to work. There are no errors.

2 answers

✓ Accepted answer

The problem is that you set isAnim = true on line 16 then compare isAnim == true on line 18 so your code will run the second if statment all the time.

You also need to stop the animation track from playing which can be done by storing it as a variable.

There are also a lot of other smaller issues witht the code shown below:-

-- pls use get service as the name of the service can be changed 
local player = game:GetService("Players").LocalPlayer

-- this is an older style wait there are better ways to do this now using :Wait()
-- repeat wait() until player.Character.Humanoid
local char = player.Character or player.CharacterAdded:Wait() -- get the player or wait for the character added event to run which will return the characters model

local Sitanim = Instance.new("Animation")
Sitanim.AnimationId = "http://www.roblox.com/asset/?id=2519769373"

script.Parent.MouseButton1Click:connect(function()

end)

When using LoadAnimation a AnimationTrack object is returned which is what we use to play and stop the animation. It also contains a state called IsPlaying which we can use.

local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait() 
local humanoid = char:WaitForChild("Humanoid") -- forgot to add this in ops

local Sitanim = Instance.new("Animation")
Sitanim.AnimationId = "http://www.roblox.com/asset/?id=182436935"

-- pre loaded 
local animationTrack = humanoid:LoadAnimation(Sitanim) 

script.Parent.MouseButton1Click:connect(function()
    if animationTrack.IsPlaying then
        animationTrack:Stop() -- using the animation track stop the animation 
    else
        -- some animation replication issues mean that you need to load it each time
        animationTrack = humanoid:LoadAnimation(Sitanim) 
        animationTrack:Play() 
    end
end)

I hope this helps.

13 comments

0

This doesn`t works. humanoid is a nil value.

0

Forgot to add that in lol. You will also need to add in the correct logic for when the player respawns but that depends on where this local script is located.

0

You will need a CharacterAdded event to assing the new humanoid object to the variable humanoid.

0

Its a gui button, its located in StarterGUI. Also what do you mean when player respawns?

0

You only account for when the player first spawns. When they die and respawn a new character is created. You just need to find the newly created humanoid as assing it to the variable so that your code will work correctly when the player dies.

0

Oh, thanks I got it. But one thing, when I am pressing the button, nothing happens. The animation wont play.

0

no errors btw

0

Are there any errors?

0

Nope, just a button that doesn`t works when you click on it.

0

Just had to invert the logic so that IsPlaying would stop else play. I have also changed the id so that I can get it working.

0

Thanks man. It works :D. Also if I want the animation to "work" more slowly do I have to add more seconds in the animation editor? Like, if I want the character to sit slowly, for example..

0

The function Play allows you to specify the speed specify https://www.robloxdev.com/api-reference/function/AnimationTrack/Play :Play(0.1, 1, [speed]) The speed is a % so 2 will be *2 speed.

0

There is also a AdjustSpeed function if you want to set it in one place. Then just use :Play() as normal.

--// Sit Animation

local player = game.Players.LocalPlayer
repeat wait() until player.Character.Humanoid
local humanoid = player.Character.Humanoid
local mouse = player:GetMouse()

local Sitanim = Instance.new("Animation")
Sitanim.AnimationId = "http://www.roblox.com/asset/?id=2519769373" -- id of animation
isAnim = false
local playSitAnim = humanoid:LoadAnimation(Sitanim) --Put it up here instead

script.Parent.MouseButton1Click:connect(function()
    if isAnim == false then 
        playSitAnim:Play()
        isAnim = true
    end
    if isAnim == true then
        playSitAnim:Stop()
        isAnim = false
    end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.