UI not refreshing unless reopened

No errors. (not the most organized, or easiest to read. this is what works, other than refreshing obviously, Im also too lazy to make it simplier)

local ui = script.Parent
local container = ui:WaitForChild("Container")
local confirmation = ui:WaitForChild("Confirmation")

local buttons = container:WaitForChild("Buttons")
local pages = container:WaitForChild("Pages")

local player = game.Players.LocalPlayer

-- functions

function OpenPage(page)
	for _, v in pages:GetChildren() do
		if v:IsA("ScrollingFrame") then
			v.Visible = true
		end
	end

	for _, v in pages:GetChildren() do
		if v:IsA("ScrollingFrame") and v.Name ~= page.Name then
			v.Visible = false
		end
	end

	Refresh()
end

function Refresh()
	for _, newpage in pages:GetChildren() do
		if newpage:IsA("ScrollingFrame") then
			local pagetype = newpage.Name
			for _, button in newpage:GetChildren() do
				if button:IsA("TextButton") then
					local bool = player:WaitForChild("PlayerData"):WaitForChild(pagetype):WaitForChild(string.lower(button.Name))
					if bool.Value == true then
						button.Indicator.BackgroundColor3 = Color3.fromRGB(0, 255, 0)
					else
						button.Indicator.BackgroundColor3 = Color3.fromRGB(255, 0, 0)
					end
				end
			end
		end
	end
end

function CheckOwnership(bool)
	if bool.Value == true then
		return true
	else
		return false
	end
end

function Confirmation(msg, price)
	container.Visible = false
	confirmation.Visible = true

	confirmation.Description.Text = msg

	local confirmed

	confirmation.Cancel.MouseButton1Click:Connect(function()
		container.Visible = true
		confirmation.Visible = false


		confirmation.Description.Text = "I love hotdogs"

		container.Visible = true
		confirmation.Visible = false

		confirmed = false
	end)

	confirmation.Confirm.MouseButton1Click:Connect(function()
		container.Visible = true
		confirmation.Visible = false

		if player.leaderstats.Wins.Value < price then
			require(game:GetService("ReplicatedStorage").Notification).Notify(player, "Insufficent Wins.", 3, Color3.new(1,0,0))

			confirmed = false
		elseif player.leaderstats.Wins.Value >= price then
			confirmed = true
		end

		confirmation.Description.Text = "I love hotdogs"

		container.Visible = true
		confirmation.Visible = false
	end)

	repeat
		task.wait(1)
	until confirmed ~= nil

	return confirmed
end

-- button toggles

for _, button in buttons:GetChildren() do
	button.MouseButton1Click:Connect(function()
		print("open")
		OpenPage(button)
	end)
end

-- pages

ui:GetPropertyChangedSignal("Enabled"):Connect(function()
	if ui.Enabled then
		Refresh()
	end
end)

task.delay(.1, function()
	for _, page in pages:GetChildren() do
		local pagetype = page.Name
		for _, button in page:GetChildren() do
			if button:IsA("TextButton") then

				local bool = player:WaitForChild("PlayerData"):FindFirstChild(pagetype):FindFirstChild(string.lower(button.Name))
				button.MouseButton1Click:Connect(function()
					local owned = CheckOwnership(bool)
					task.wait(.05)
					if owned == true then return end

					local confirmed = Confirmation("Would you like to Purchase "..button.Name.." for "..button:GetAttribute("Price").." Wins?", button:GetAttribute("Price"))

					task.wait(.05)

					if confirmed == true then
						game:GetService("ReplicatedStorage").RequestValue:FireServer(bool, button:GetFullName())
					end

					Refresh()
				end)
			end
		end
	end
end)

Thanks for reading and Thank again if you reply!

1 Like

just run the refresh function in a loop while the ui is visible

Not a fan of that idea for optimization.

then you could use .AncestryChanged on PlayerData and refresh it when the value of something gets changed

could you give me something to look at to understand you better?

1 Like

edited your code quickly

local ui = script.Parent
local container = ui:WaitForChild("Container")
local confirmation = ui:WaitForChild("Confirmation")

local buttons = container:WaitForChild("Buttons")
local pages = container:WaitForChild("Pages")

local player = game.Players.LocalPlayer

local RefreshConnections = {}

-- functions

function OpenPage(page)
	for _, v in pages:GetChildren() do
		if v:IsA("ScrollingFrame") then
			v.Visible = true
		end
	end

	for _, v in pages:GetChildren() do
		if v:IsA("ScrollingFrame") and v.Name ~= page.Name then
			v.Visible = false
		end
	end

	Refresh()
end

function DisableConnections()
	for i,v in pairs(RefreshConnections) do 
		v:Disconnect()
	end
end

function Refresh()
	for _, newpage in pages:GetChildren() do
		if newpage:IsA("ScrollingFrame") then
			local pagetype = newpage.Name
			
			RefreshConnections[#RefreshConnections+1] = newpage.AncestryChanged:Connect(function(inst, parent)
				local button = newpage:FindFirstChild(inst.Name) --// idk how your buttons are named so you might need to change this
				if inst.Value == true and button ~= nil then
					button.Indicator.BackgroundColor3 = Color3.fromRGB(0, 255, 0)
				else
					button.Indicator.BackgroundColor3 = Color3.fromRGB(255, 0, 0)
				end
			end)
			
			for _, button in newpage:GetChildren() do
				if button:IsA("TextButton") then
					local bool = player:WaitForChild("PlayerData"):WaitForChild(pagetype):WaitForChild(string.lower(button.Name))
					if bool.Value == true then
						button.Indicator.BackgroundColor3 = Color3.fromRGB(0, 255, 0)
					else
						button.Indicator.BackgroundColor3 = Color3.fromRGB(255, 0, 0)
					end
				end
			end
		end
	end
end

function CheckOwnership(bool)
	if bool.Value == true then
		return true
	else
		return false
	end
end

function Confirmation(msg, price)
	container.Visible = false
	confirmation.Visible = true

	confirmation.Description.Text = msg

	local confirmed

	confirmation.Cancel.MouseButton1Click:Connect(function()
		container.Visible = true
		confirmation.Visible = false


		confirmation.Description.Text = "I love hotdogs"

		container.Visible = true
		confirmation.Visible = false

		confirmed = false
	end)

	confirmation.Confirm.MouseButton1Click:Connect(function()
		container.Visible = true
		confirmation.Visible = false

		if player.leaderstats.Wins.Value < price then
			require(game:GetService("ReplicatedStorage").Notification).Notify(player, "Insufficent Wins.", 3, Color3.new(1,0,0))

			confirmed = false
		elseif player.leaderstats.Wins.Value >= price then
			confirmed = true
		end

		confirmation.Description.Text = "I love hotdogs"

		container.Visible = true
		confirmation.Visible = false
	end)

	repeat
		task.wait(1)
	until confirmed ~= nil

	return confirmed
end

-- button toggles

for _, button in buttons:GetChildren() do
	button.MouseButton1Click:Connect(function()
		print("open")
		OpenPage(button)
	end)
end

-- pages

ui:GetPropertyChangedSignal("Enabled"):Connect(function()
	if ui.Enabled then
		Refresh()
	else 
		DisableConnections()
	end
end)

task.delay(.1, function()
	for _, page in pages:GetChildren() do
		local pagetype = page.Name
		for _, button in page:GetChildren() do
			if button:IsA("TextButton") then

				local bool = player:WaitForChild("PlayerData"):FindFirstChild(pagetype):FindFirstChild(string.lower(button.Name))
				button.MouseButton1Click:Connect(function()
					local owned = CheckOwnership(bool)
					task.wait(.05)
					if owned == true then return end

					local confirmed = Confirmation("Would you like to Purchase "..button.Name.." for "..button:GetAttribute("Price").." Wins?", button:GetAttribute("Price"))

					task.wait(.05)

					if confirmed == true then
						game:GetService("ReplicatedStorage").RequestValue:FireServer(bool, button:GetFullName())
					end

					Refresh()
				end)
			end
		end
	end
end)

i like @CZXPEK 's idea with the RefreshConnections but this code could look a lot cleaner

local GUI = script.Parent

local Holder = GUI:WaitForChild("Container")
local Confirmation = GUI:WaitForChild("Confirmation")
local Buttons = Holder:WaitForChild("Buttons")
local Pages = Holder:WaitForChild("Pages")

local Players = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage")

local LocalPlayer = Players.LocalPlayer
local RefreshConnections = {}

-- FUNCTIONS

function GetPageValue(PageType, Name)
	return LocalPlayer:WaitForChild("PlayerData"):FindFirstChild(PageType):FindFirstChild(Name).Value
end

function Disconnect()
	for _, Connection in RefreshConnections do 
		Connection:Disconnect()
	end
end

function Refresh()
	local function UpdateIndicator(Button, PageType)
		Button.Indicator.BackgroundColor3 = (
			GetPageValue(PageType, Button.Name:lower()) and 
			Color3.fromRGB(0, 255, 0) or 
			Color3.fromRGB(255, 0, 0)
		)
	end
	
	for _, NewPage in Pages:GetChildren() do
		if not NewPage:IsA("ScrollingFrame") then continue end
		
		for _, Button in NewPage:GetChildren() do
			if not Button:IsA("TextButton") then continue end
			
			RefreshConnections[#RefreshConnections + 1] = NewPage.AncestryChanged:Connect(function(Button, Page)
				UpdateIndicator(Button, Page.Name)
			end)
			
			UpdateIndicator(Button, NewPage.Name)
		end
	end
end

function OpenPage(TargetPage)
	for _, Page in Pages:GetChildren() do
		if not Page:IsA("ScrollingFrame") then continue end
		
		Page.Visible = (Page.Name == TargetPage.Name)
	end; Refresh()
end

function Confirmation(Message, Price)
	Holder.Visible = false
	Confirmation.Visible = true
	Confirmation.Description.Text = Message

	local Confirmed

	Confirmation.Cancel.MouseButton1Click:Connect(function()
		Holder.Visible = true
		Confirmation.Visible = false
		Confirmation.Description.Text = "I love hotdogs"
		Confirmed = false
	end)

	Confirmation.Confirm.MouseButton1Click:Connect(function()
		local CanPurchase = LocalPlayer.leaderstats.Wins.Value >= Price
		
		Holder.Visible = true
		Confirmation.Visible = false
		
		if not CanPurchase then
			require(RS.Notification).Notify(LocalPlayer, "Insufficent wins", 3, Color3.new(1,0,0))
		end

		Confirmation.Description.Text = "I love hotdogs"
		Confirmed = CanPurchase
	end)

	repeat
		wait()
	until Confirmed ~= nil

	return Confirmed
end

-- BUTTON TOGGLES

for _, Button in Buttons:GetChildren() do
	Button.MouseButton1Click:Connect(function()
		print("Open page:", Button.Name)
		OpenPage(Button)
	end)
end

-- PAGES

GUI:GetPropertyChangedSignal("Enabled"):Connect(function()
	if not GUI.Enabled then Disconnect() return end
	Refresh()
end)

for _, Page in Pages:GetChildren() do
	local PageType = Page.Name
	
	for _, Button in Page:GetChildren() do
		if not Button:IsA("TextButton") then continue end
		
		Button.MouseButton1Click:Connect(function()
			local Owned = GetPageValue(PageType, Button.Name:lower()); if Owned then return end
			local ConfirmationMessage = "Would you like to Purchase ".. Button.Name .." for ".. Button:GetAttribute("Price") .." Wins?"
			
			if Confirmation(ConfirmationMessage, Button:GetAttribute("Price")) then
				RS.RequestValue:FireServer(Owned, Button:GetFullName())
			end

			Refresh()
		end)
	end
end

i don’t expect this to work

problem persists. where the ui doesnt refresh.

before reopen
image
after reopen.
image

1 Like