Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why cant I get this button to stop the animation?

Asked by 5 years ago

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

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

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.

0
This doesn`t works. humanoid is a nil value. HeyItzDanniee 252 — 5y
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. User#5423 17 — 5y
0
You will need a CharacterAdded event to assing the new humanoid object to the variable humanoid. User#5423 17 — 5y
0
Its a gui button, its located in StarterGUI. Also what do you mean when player respawns? HeyItzDanniee 252 — 5y
View all comments (9 more)
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. User#5423 17 — 5y
0
Oh, thanks I got it. But one thing, when I am pressing the button, nothing happens. The animation wont play. HeyItzDanniee 252 — 5y
0
no errors btw HeyItzDanniee 252 — 5y
0
Are there any errors? User#5423 17 — 5y
0
Nope, just a button that doesn`t works when you click on it. HeyItzDanniee 252 — 5y
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. User#5423 17 — 5y
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.. HeyItzDanniee 252 — 5y
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. User#5423 17 — 5y
0
There is also a AdjustSpeed function if you want to set it in one place. Then just use :Play() as normal. User#5423 17 — 5y
Ad
Log in to vote
0
Answered by
Nikkulaos 229 Moderation Voter
5 years ago
--// 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)

Answer this question