Dance GUI - Multiple Page Activation

I am making a dance GUI for my game with 2 seperate pages. The code only works for one page, in this case page 1. How can I make the dance script function for both pages at the same time? This is my best effort below, and it did not work, still only triggering page 1.

local dances = script.Parent.Pages.Page1 or
	script.Parent.Pages.Page2

You might be able to use a for loop to do this…

for _, dances in pairs(script.Parent.Pages:GetChildren()) do
      -- Code here
end

This allows you to add a bunch of pages without needing to touch the code, and if you have v form the loop be dances, you don’t need to edit your original dance GUI code.

This is what the script looks like-still doesn’t work…unfortunately

for _, dances in pairs(script.Parent.Pages:GetChildren()) do
	local dances = script.Parent.Pages.Page1 or
		script.Parent.Pages.Page2
end

We already got this variable from the loop, remove this and have the rest of your code below, like

for _, dances in pairs(script.Parent.Pages:GetChildren()) do
     -- You're GUI code goes inside here, no need to make the local dances var
end

What do you mean by GUI code? Which code…

Your dance GUI code. You know, the code that makes your dances actually work.

This is what it currently looks like with my whole script. The first page dances work but the second page is still unresponsive.

for _, dances in pairs(script.Parent.Pages:GetChildren()) do
	local dances = script.Parent.Pages.Page1 or
		script.Parent.Pages.Page2
	local Players = game:GetService("Players")
	local Player = Players.LocalPlayer
	local anim
	local playing = false

	-- We will pause the script to wait for the chracter to load

	repeat wait()

	until Player.Character
	local char = Player.Character
	local Humanoid = char:WaitForChild("Humanoid")

	-- Creat a Animtion In The Player

	local danceAnimation = Instance.new("Animation")
	danceAnimation.Parent = char
	danceAnimation.Name = "The Robot"

	-- Setup the animtion to play

	local function setupAnimation (id)
		if playing then return end
		playing = true
		local selectedId = "rbxassetid://"..id
		danceAnimation.AnimationId = selectedId
		anim = Humanoid:LoadAnimation(danceAnimation)
		anim:Play()
	end

	-- Stops the current animation playing

	script.Parent.Stop.MouseButton1Click:Connect(function()
		if anim then
			anim:Stop()
			playing = false
		end
	end)

	-- detects when a button is pressed

	for _,v in pairs(dances:GetChildren()) do
		if v:IsA("TextButton") then
			v.MouseButton1Click:Connect(function()
				setupAnimation(v.Name)
			end)
		end
	end
end

I said remove this variable, the variable is already there. Keep your script just remove this.

Forgot that was still there!!

Works great now, many many thanks sir.

1 Like