Playing an animation freezes my game!?

So I don’t really know why this is happening but its really annoying so I have a GUI that when clicked should load an animation and then play it, but every time its clicked it freezes the game.

script:

player = game.Players.LocalPlayer.Character
animation = player.Humanoid:LoadAnimation(script.AirGuitar)
gui = script.Parent.Parent.Parent.DanceGUI
button = script.Parent.Parent.Parent.Emote
animation.Looped = true
local playing = false

script.Parent.MouseButton1Click:Connect(function()
	wait()
	if playing == false then
		gui.Visible = false
		button.Visible = true
		playing = true
		animation:Play()
	else
		if playing == true then
			animation:Stop()
			gui.Visible = false
			button.Visible = true
			playing = false
		end
	end
	
end)

Can anyone help me fix this problem.

Are there any output errors? If so, can you share them? Also, are you sure the freeze is coming from this exact script? there might be other causes to this.

I’ve tested it, no errors its just that when the you play the animation your screen freezes and nothing happens.

Try printing stuff on different part of the script, to see if it runs completely, so for example:

script.Parent.MouseButton1Click:Connect(function()
    print("clicked")
	wait()
    print("done waiting")
	if playing == false then
        print("playing==false")
		playing = true
		animation:Play()
	else
		if playing == true then
            print("playing==true")
			animation:Stop()
			playing = false
		end
	end
	 
    print("event excecuted")
end)

if it somehow freezes, the script should be stuck at some point, (btw, you also don’t need to specify things like gui.Visible = true, if you don’t touch it, it will remain the same)