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.
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.
--// 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)