How can I detect the current page of a UIPageLayout?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? I would like to know the current page so that each time it changes, a sound plays

  2. What is the issue? I tried that but it didn’t work

local testA
testA = UIPageLayout:GetPropertyChangedSignal("CurrentPage"):Connect(function()
	warn("WORK")
	Sound:Play()
end)

local testB
testB = UIPageLayout.PageEnter:Connect(function()
	warn("WORK")
	Sound:Play()
end)

The script :

function OpenCase(itemProperty)
	local Sound = WaitForPath(MainGui,"Case.TickSound")
	local ItemsCase = WaitForPath(MainGui,"Case.ItemsCase")
	local UIPageLayout :UIPageLayout = WaitForPath(ItemsCase,"UIPageLayout")

	for _, children in ItemsCase:GetChildren() do
		if children:IsA("UIPageLayout") or children.Name == "Template" then continue end
		children:Destroy()
	end

	local itemSlot = math.random(75, 125)
	local timeCase = random:NextNumber(6, 8)
	local itemAddBeforeAndAfterItem = 2
	local itemsCaseGap = random:NextNumber(0, 15)/100

	ItemsCase.Position = UDim2.fromScale(0.5, 0.5)
	UIPageLayout.EasingStyle = Enum.EasingStyle.Exponential

	local itemInstance :Frame
	
	for i = 1, itemSlot + itemAddBeforeAndAfterItem do
		if i == itemSlot then 
			itemInstance = AddItemInCase(i, true)
		else
			AddItemInCase(i, false)
		end
	end
	
	RunService.PostSimulation:Wait()

	UIPageLayout.Animated = false
	UIPageLayout.TweenTime = 0
	UIPageLayout:JumpToIndex(itemAddBeforeAndAfterItem)

	task.wait(1)

	UIPageLayout.Animated = true
	UIPageLayout.TweenTime = timeCase
	UIPageLayout:JumpToIndex(itemSlot - 1)

	if random:NextNumber() > 0.5 then
		ItemsCase.Position = UDim2.fromScale(0.5 + itemsCaseGap, 0.5)
	else
		ItemsCase.Position = UDim2.fromScale(0.5 - itemsCaseGap, 0.5)
	end
	
	UIPageLayout.Stopped:Once(function()
		warn("FINISH")
	end)

	local testA
	testA = UIPageLayout:GetPropertyChangedSignal("CurrentPage"):Connect(function()
		warn("WORK")
		Sound:Play()
	end)

	local testB
	testB = UIPageLayout.PageEnter:Connect(function()
		warn("WORK")
		Sound:Play()
	end)
end
1 Like

Literally just UIPageLayout.CurrentPage. All you have to do is connect a GetPropertyChangedSignal event to that.

To detect the current page of a UIPageLayout , you can use the CurrentPage property and connect to its Changed event. In your case, you can connect a function to play the sound whenever the current page changes.

The script:

function OpenCase(itemProperty)
    local Sound = WaitForPath(MainGui, "Case.TickSound")
    local ItemsCase = WaitForPath(MainGui, "Case.ItemsCase")
    local UIPageLayout = WaitForPath(ItemsCase, "UIPageLayout")

    for _, children in ItemsCase:GetChildren() do
        if children:IsA("UIPageLayout") or children.Name == "Template" then continue end
        children:Destroy()
    end

    local itemSlot = math.random(75, 125)
    local timeCase = random:NextNumber(6, 8)
    local itemAddBeforeAndAfterItem = 2
    local itemsCaseGap = random:NextNumber(0, 15) / 100

    ItemsCase.Position = UDim2.fromScale(0.5, 0.5)
    UIPageLayout.EasingStyle = Enum.EasingStyle.Exponential

    local itemInstance

    for i = 1, itemSlot + itemAddBeforeAndAfterItem do
        if i == itemSlot then
            itemInstance = AddItemInCase(i, true)
        else
            AddItemInCase(i, false)
        end
    end

    RunService.PostSimulation:Wait()

    UIPageLayout.Animated = false
    UIPageLayout.TweenTime = 0
    UIPageLayout:JumpToIndex(itemAddBeforeAndAfterItem)

    task.wait(1)

    UIPageLayout.Animated = true
    UIPageLayout.TweenTime = timeCase
    UIPageLayout:JumpToIndex(itemSlot - 1)

    if random:NextNumber() > 0.5 then
        ItemsCase.Position = UDim2.fromScale(0.5 + itemsCaseGap, 0.5)
    else
        ItemsCase.Position = UDim2.fromScale(0.5 - itemsCaseGap, 0.5)
    end

    UIPageLayout.Stopped:Connect(function()
        warn("FINISH")
    end)

    local currentPageConnection
    currentPageConnection = UIPageLayout:GetPropertyChangedSignal("CurrentPage"):Connect(function()
        local currentPage = UIPageLayout.CurrentPage
        print("Current Page:", currentPage)

        Sound:Play()
    end)

    local testB
    testB = UIPageLayout.PageEnter:Connect(function()
        warn("WORK")
        Sound:Play()
    end)
end

the currentPageConnection variable stores the connection to the Changed event of the CurrentPage property. The connected function prints the current page and plays the sound. Additionally, the event is disconnected when the UIPageLayout stops (reaches its final state).

The issue

The issue with the page script seems to be that the PageEnter event might not be firing as expected. The PageEnter event is triggered when a new page becomes the active page, but in your case, it might not be working correctly.
To ensure that the sound plays when the page changes, I modified the script to include a connection to the CurrentPage property’s Changed event. This connection will trigger the sound whenever the current page changes.

It doesn’t work if you look closely above, I’ve already tried.
The problem is that UIPageLayout.CurrentPage is the page which will be the destination, for example according to the image below I am on the red frame but according to UIPageLayout I am on the green frame because the animation is not finished whereas I would really like the real current page which is the red one in my example.

image