Any way to improve code?

I was wondering if there is anyway to optimize this code? Any and all help is very appreciated!

local player = game:GetService("Players").LocalPlayer
local danceBox = script.Parent.danceBox
local PlayerModule = require(script.Parent.PlayerModule)
local MPS = game:GetService("MarketplaceService")
local gamepassID = script.Parent:GetAttribute("ID")
local Animations = {8032620426, 8032633886, 8032656088}
local character = player.Character or player.CharacterAdded:Wait()
local CollectionService = game:GetService("CollectionService")
local toServerAnimation = game:GetService("ReplicatedStorage"):WaitForChild("toServerAnimation");

local function cloneButton()
	for _, id in pairs(Animations) do
		local clone = danceBox:Clone()
		clone.Visible = true
		clone.Parent = script.Parent.ScrollingFrame
		local viewport = clone.ViewportFrame
		local iddata = MPS:GetProductInfo(id)
		clone.TextLabel.Text = iddata.Name
		PlayerModule:Create(player,viewport)
		clone:SetAttribute("ID", id)
		local animation = Instance.new("Animation")
		animation.AnimationId = "http://www.roblox.com/asset/?id=" .. clone:GetAttribute("ID")
		local character = clone.ViewportFrame.WorldModel.Dummy
		local track = character.Humanoid:LoadAnimation(animation)
		track.Looped = true
		track:Play()
	end
end

local haspass
local function promtPurchase()
	haspass = false
	local success, message = pcall(function()
		haspass = MPS:UserOwnsGamePassAsync(player.UserId, gamepassID)
	end)
	if haspass == true then 
		print("has pass")
	else
		MPS:PromptGamePassPurchase(player, gamepassID)
	end
end

local Free = script.Parent:GetAttribute("Free")

local function buttonClick(object)
	object.MouseButton1Down:Connect(function()
		script.Parent.Clicksound:Play()
		local charId = character:GetAttribute("ID")
		local id = object:GetAttribute("ID")
		if charId ~= id then
			local value = false
			toServerAnimation:FireServer(id, value)
		elseif charId == id then
			local value = true
			toServerAnimation:FireServer(id, value)
		end
	end)
end

local button = coroutine.wrap(function()
	cloneButton()

	for _, object in pairs(CollectionService:GetTagged("danceBox")) do
		buttonClick(object)
	end
end)
local called = false
game:GetService("Players").LocalPlayer.PlayerGui:WaitForChild("Opener2").TextButton.MouseButton1Down:Connect(function()
	if called == false then
		button()
		called = true
	elseif called ~= false then
	end
	print(MPS:UserOwnsGamePassAsync(player.UserId, gamepassID))
	script.Parent.ClickSound2:Play()
	if MPS:UserOwnsGamePassAsync(player.UserId, gamepassID) == true or Free == true then
		if script.Parent.Enabled == false  then
			script.Parent.Enabled = true
		elseif script.Parent.Enabled == true  then
			script.Parent.Enabled = false 
		end
	elseif MPS:UserOwnsGamePassAsync(player.UserId, gamepassID) ==  false then
		promtPurchase()
	end

end)


CollectionService:GetInstanceAddedSignal("danceBox"):Connect(function(Instance)
	buttonClick(Instance)
end)

What does this code do? Please be more descriptive.

1 Like
CollectionService:GetInstanceAddedSignal("danceBox"):Connect(buttonClick)