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

Click once, animation plays, click another, animation stops. Help?

I'm making a dance simulator. And I need dances right? So I wanted to make a gui that plays a dance when you click it, when you click it again, it stops. I tried doing this:

function click()
local Animation = Instance.new("Animation")
local animTrack = game.Player.LocalPlayer.Character.Humanoid:LoadAnimation(Animation)
Animation.AnimationId = "http://www.roblox.com/asset?id=234539352"
animTrack:Play()
if script.Parent.MouseButton1Click and animTrack.Playing == true then
	animTrack:Stop()
end
end
script.Parent.MouseButton1Click:connect(click)

None of the script works.

I got this from the output:

Player is not a member of DataModel

Can anyone help me?

2 answers

Small Typo with 'Player'

'Player' should be 'Players', also no need for the conditional check on MouseButton1Click, and also the reason this is occurring is because you're stopping it each time you play it.

Try using some debounces!

Also, we don't want to keep loading in new Instances to the player each time they click the button! Let's try something as so:

Hint, sometimes using functions to organize things can help a lot. Here's a way I would organize it to make it neater for future scripts as-well.

local plr = game.Players.LocalPlayer
local chr = plr.Character or plr.CharacterAdded:wait()
local Animation;
local animTrack;
local playing = false;

function newAnimation(Id)
Animation = Instance.new("Animation")
animTrack = chr["Humanoid"]:LoadAnimation(Animation)
Animation.AnimationId = "http://www.roblox.com/asset?id=" .. Id
end

function hasAnimation(Id)
if chr:FindFirstChild("Animation") and chr["Animation"].AnimationId == "http://www.roblox.com/asset?id="..Id then
return true
end
return false
end

function stop()
animTrack:Stop()
playing = false;
end

function play()
animTrack:Play()
playing = true;
end

script.Parent.MouseButton1Down:connect(function()
print("Clicked!")
if not hasAnimation(234539352) then
newAnimation(234539352)
end
if not playing then
play()
elseif playing then
stop()
end
end)


Code Above Is Untested

8 comments

0

It didn't work, there was nothing in the output.

0

Is this in a local-script?

0

Ah, no. I'll change it to a local script.

0

Remember, whenever you see 'LocalPlayer', that means that it has to be a local-script for it to work.

0

It doesn't work, still. :/

0

Any output?

0

There is no output.

0

Any idea of what to do?

Simple answer: You forgot the 's' in "Players" on line 3.

I'm not sure if this'll fix the entire thing, but from what I see, it should. Hope I helped :P

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