Sound Not Playing On Keyframe Reached

Alright, so I am working on a little emote game, and I am trying to make the player have a clapping sound when the animation reaches the Keyframe named “clap” to play the sound. Everything looks fine, but then an error shows up on the function OnUnEquipped() part. Here’s my code:

enabled = false
local Tool = script.Parent;
local dancer = nil
local player = game.Players.LocalPlayer



function onActivated()


	--This will check if it is enabled and then either start dancing or stop dancing
if enabled == false then
	local humanoid = Tool.Parent:FindFirstChild("Humanoid")
	local torso = Tool.Parent:FindFirstChild("UpperTorso")

	dancer = humanoid:LoadAnimation(Tool.dance)
	Tool.DanceObject.Value = dancer
	dancer:Play()
	dancer:AdjustSpeed(1.01)
	dancer.KeyframeReached:connect(function(keyframe) -- Wait 'til we reach a keyframe
	if keyframe == "clap" then -- if the keyframe is 'tick' then play the pin sound
	Tool.dancepart.Clap:Play()
	Tool.dancepart.Position = player.Character.Head.Position
	Tool.dancepart.SongLoop:Play()
	enabled = true
	speed = player.Character.Humanoid.WalkSpeed
	player.Character.Humanoid.WalkSpeed = 0
	player.Character.Humanoid.JumpPower = 0
	repeat 
	wait()
	Tool.dancepart.Position = player.Character.Head.Position
	until enabled == false
	
else
	dancer:Stop()
	dancer:remove()
	Tool.dancepart.SongLoop:Stop()
	enabled = false
	player.Character.Humanoid.WalkSpeed = speed
	player.Character.Humanoid.JumpPower = 50
end
end

function onUnequipped()
		dancer:Stop()
		dancer:remove()
		Tool.dancepart.SongLoop:Stop()
		enabled = false
		player.Character.Humanoid.WalkSpeed = speed
		player.Character.Humanoid.JumpPower = 50
end

Tool.Activated:connect(onActivated)
Tool.Unequipped:connect(onUnequipped)

Quick question, what does the error say in the output?

1 Like

You forgot to end your if statement which started on line 20

Here is a fixed version:

enabled = false
local Tool = script.Parent;
local dancer = nil
local player = game.Players.LocalPlayer

function onActivated()
--This will check if it is enabled and then either start dancing or stop dancing
	if enabled == false then
		local humanoid = Tool.Parent:FindFirstChild("Humanoid")
		local torso = Tool.Parent:FindFirstChild("UpperTorso")
	
		dancer = humanoid:LoadAnimation(Tool.dance)
		Tool.DanceObject.Value = dancer
		dancer:Play()
		dancer:AdjustSpeed(1.01)
		dancer.KeyframeReached:connect(function(keyframe) -- Wait 'til we reach a keyframe
			if keyframe == "clap" then -- if the keyframe is 'tick' then play the pin sound
				Tool.dancepart.Clap:Play()
				Tool.dancepart.Position = player.Character.Head.Position
				Tool.dancepart.SongLoop:Play()
				enabled = true
				speed = player.Character.Humanoid.WalkSpeed
				player.Character.Humanoid.WalkSpeed = 0
				player.Character.Humanoid.JumpPower = 0
				repeat 
				wait()
				Tool.dancepart.Position = player.Character.Head.Position
				until enabled == false
			end
		end)
	else
		dancer:Stop()
		dancer:remove()
		Tool.dancepart.SongLoop:Stop()
		enabled = false
		player.Character.Humanoid.WalkSpeed = speed
		player.Character.Humanoid.JumpPower = 50
	end
end

function onUnequipped()
	dancer:Stop()
	dancer:remove()
	Tool.dancepart.SongLoop:Stop()
	enabled = false
	player.Character.Humanoid.WalkSpeed = speed
	player.Character.Humanoid.JumpPower = 50
end

Tool.Activated:connect(onActivated)
Tool.Unequipped:connect(onUnequipped)

The “else” in there is underlined as an error.

I got that result using his updated code.

This is because the “dancer” variable is only set in the first part of the if statement, not in the else part.

Then what am I supposed to do?

This code is very deprecated:

  • Instance:remove has been superceded by Instance:Destroy, which also prevents memory leaks.

  • AnimationTrack.KeyframeReached has been superceded by AnimationTrack:GetMarkerReachedSignal, which will require a recompiling of your animation although it’s still very much worth it to use.

  • Event:connect has been superceded by Event:Connect, nothing more to really add.

  • All of your functions are global, which will always slow down processing time, just prefix them with local. Do this for the enabled variable as well.

Your code could use an optimizations (and fixes!):

  • The AnimationTrack attached to the dancer variable needs to be defined whenever the character changes, this can be done on the Tool.Equipped event.

  • The same applies for the humanoid and torso variables, they only need to be defined whenever the character changes, this can be connected to the same event.

  • speed was defined as a global variable since it was first created in the dancer.KeyframeReached event connection. You should define it as local to the script, nexto Tool, dancer, and all those other variables at the beginning.

  • the connection to dancer.KeyframeReached has to be disconnected, or it will cause a memory leak.

2 Likes

I would rather still have deprecated code. I just need help fixing up the error at the bottom.

So, I’ll just quote the important part:

Can you please address anywhere in the script that actually needs the changes?

You could probably just add this to the end of your code:

Tool.Equipped:Connect(function()
	local humanoid = Tool.Parent:FindFirstChild("Humanoid")
	dancer = humanoid:LoadAnimation(Tool.dance)
end)

Look to my updated code provided above in my last reply.

1 Like

Alright, it turns out that your updated code in which you have generously (It’s like I am writing a 500 word essay.) provided for me is working stupendously. But whenever the clap sound plays, the song starts over after that. Who needs the song anyway, it’s copyrighted from Fortnite.

Try using the Sound:Pause and Sound:Resume methods respectively, instead of Sound:Stop and Sound:Play.

Thanks for the offer, But that would eventually desync the song with the claps, and I already got the clapping sounds, in which there are already clapping sound effects in the song, so I don’t need the song playing anymore.

1 Like

Don’t forget to mark a reply as a solution once you are finished with the thread!

Well, both threads really…

Sorry about that…