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

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

Asked by 9 years ago

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

Log in to vote
1
Answered by 9 years ago

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

0
It didn't work, there was nothing in the output. Grenaderade 525 — 9y
0
Is this in a local-script? DigitalVeer 1473 — 9y
0
Ah, no. I'll change it to a local script. Grenaderade 525 — 9y
0
Remember, whenever you see 'LocalPlayer', that means that it has to be a local-script for it to work. DigitalVeer 1473 — 9y
View all comments (4 more)
0
It doesn't work, still. :/ Grenaderade 525 — 9y
0
Any output? DigitalVeer 1473 — 9y
0
There is no output. Grenaderade 525 — 9y
0
Any idea of what to do? Grenaderade 525 — 9y
Ad
Log in to vote
0
Answered by
dyler3 1510 Moderation Voter
9 years ago

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

Answer this question