I need a help with my code

i am working on an ui of book alomst done with it

the issue here is that the book have two section1 and section2 section 1 and two both have a child and that child has a name of clone and that clone is updateing with the page
the section1 clone works fine but the section2 clone doesnt work as supposed to
section2 clone not updateing at all

local Frame = script.Parent
local Seciton1 = Frame.Seciton1
local Seciton2 = Frame.Seciton2

local NextButton = Frame.Next
local BackButton = Frame.Back
local FrameModle = script.Frame
local Page = Frame.Page
local rbx = "rbxassetid://"
local Pages = {
	Page1 = {Frame1 = {
		text = "جبالي اجا و سلمى",
		ImageId = rbx.."18222933997",
		Size = UDim2.new(0.944, 0,0.505, 0),
		Position = UDim2.new(0.015, 0,0.086, 0)
	},Frame2 = {
		text ="جديس",
		ImageId = rbx.."18222773535",
		Size = UDim2.new(0.944, 0,0.505, 0),
		Position = UDim2.new(0.066, 0,0.389, 0)
	}},
	Page2 = {Frame1 = {
		text = "ddddddddd",
		ImageId = rbx.."631727248",
		Size = UDim2.new(0.944, 0,0.505, 0),
		Position = UDim2.new(0.015, 0,0.086, 0)
	},Frame2 = {
		text ="جديس2",
		ImageId = rbx.."3333333",
		Size = UDim2.new(0.944, 0,0.505, 0),
		Position = UDim2.new(0.066, 0,0.389, 0)
	}},
	Page3 = {Frame1 = {
		text = "page3",
		ImageId = rbx.."9180622665",
		Size = UDim2.new(0.944, 0,0.505, 0),
		Position = UDim2.new(0.015, 0,0.086, 0)
	},Frame2 = {
		text ="جديس2",
		ImageId = rbx.."3333333",
		Size = UDim2.new(0.944, 0,0.505, 0),
		Position = UDim2.new(0.066, 0,0.389, 0)
	}},
}
local legnthOfDictionary = function(dictionary)
	local HowManyItems = 0
	for i in pairs(dictionary) do
		HowManyItems += 1
	end
	return HowManyItems
end

local MaxPage = legnthOfDictionary(Pages)
local CurrentPage = 1
Page.Text  = "صفحه"..tostring(MaxPage).."/"..tostring(CurrentPage)
print(Page)
local Updateing = function(PageFrame,key,IsItFirstTime)
	print(PageFrame)
	Page.Text  = "صفحه"..tostring(MaxPage).."/"..tostring(CurrentPage)
	if Seciton1:FindFirstChild("Clone") and not IsItFirstTime then

		local Clone = Seciton1:FindFirstChild("Clone")
		Clone.Position =  PageFrame.Position
		Clone.Size = PageFrame.Size
		Clone:WaitForChild("image").Image = PageFrame.ImageId
		Clone:WaitForChild("Name").Text = PageFrame.text
		print("Clone1 Edited, ",Clone)

		return
	elseif Seciton2:FindFirstChild("Clone") and not IsItFirstTime  then
		local Clone = Seciton2:FindFirstChild("Clone")
		print(Clone,"Edited?2")

		Clone.Position =  PageFrame.Position
		Clone.Size = PageFrame.Size
		Clone:WaitForChild("image").Image = PageFrame.ImageId
		Clone:WaitForChild("Name").Text = PageFrame.text
		print("Clone2 Edited, ",Clone)
		return
	end
	local Clone = FrameModle:Clone()
	Clone.Name = "Clone"
	Clone.Position =  PageFrame.Position
	Clone.Size = PageFrame.Size
	Clone:WaitForChild("image").Image = PageFrame.ImageId
	Clone:WaitForChild("Name").Text = PageFrame.text
	print(key)
	if key == 1 then
		Clone.Parent = Seciton1
	else
		Clone.Parent = Seciton2
	end
end
Updateing(Pages["Page1"].Frame1,1,true)
Updateing(Pages["Page1"].Frame2,2,true)

local NextBackFunction = function(Next)
	if Next then
		if CurrentPage ~= MaxPage then
			CurrentPage += 1
			local TargetPage = Pages["Page"..CurrentPage]
			for key,PageFrame in pairs(TargetPage) do
				print(PageFrame)
				if key == "Frame1" then
					print("called from here for Seciton1")
					Updateing(PageFrame,1,false)
				else
					print("called from here for Seciton1")

					Updateing(PageFrame,2,false)

				end
				
			end
		end
	else
		if CurrentPage ~= 1 then
			CurrentPage -= 1
			local TargetPage = Pages["Page"..CurrentPage]
			for key,PageFrame in pairs(TargetPage) do
				if key == "Frame1" then
					Updateing(PageFrame,1,false)
				else
					Updateing(PageFrame,2,false)

				end
			end
		end
		
	end
end

NextButton.MouseButton1Click:Connect(function()
	NextBackFunction(true)
end)
BackButton.MouseButton1Click:Connect(function()
	NextBackFunction(false)
end)
--for Key,Value in pairs(Pages) do
--	for _,FramePropites in pairs(Value) do
		
--	end
--end



image

I couldn’t understand a lot of it but from what im seeing

if Seciton1:FindFirstChild("Clone") and not IsItFirstTime then

		local Clone = Seciton1:FindFirstChild("Clone")
		Clone.Position =  PageFrame.Position
		Clone.Size = PageFrame.Size
		Clone:WaitForChild("image").Image = PageFrame.ImageId
		Clone:WaitForChild("Name").Text = PageFrame.text
		print("Clone1 Edited, ",Clone)

		return
	elseif Seciton2:FindFirstChild("Clone") and not IsItFirstTime  then
		local Clone = Seciton2:FindFirstChild("Clone")
		print(Clone,"Edited?2")

		Clone.Position =  PageFrame.Position
		Clone.Size = PageFrame.Size
		Clone:WaitForChild("image").Image = PageFrame.ImageId
		Clone:WaitForChild("Name").Text = PageFrame.text
		print("Clone2 Edited, ",Clone)
		return
	end

It looks like this is what controls what to show on a page. But it’s one if statement, so if the first condition works, then the second condition doesn’t run. So you’d have to split it into two if statements

if Seciton1:FindFirstChild("Clone") and not IsItFirstTime then

		local Clone = Seciton1:FindFirstChild("Clone")
		Clone.Position =  PageFrame.Position
		Clone.Size = PageFrame.Size
		Clone:WaitForChild("image").Image = PageFrame.ImageId
		Clone:WaitForChild("Name").Text = PageFrame.text
		print("Clone1 Edited, ",Clone)

		return
end
	
if Seciton2:FindFirstChild("Clone") and not IsItFirstTime  then
		local Clone = Seciton2:FindFirstChild("Clone")
		print(Clone,"Edited?2")

		Clone.Position =  PageFrame.Position
		Clone.Size = PageFrame.Size
		Clone:WaitForChild("image").Image = PageFrame.ImageId
		Clone:WaitForChild("Name").Text = PageFrame.text
		print("Clone2 Edited, ",Clone)
		return
end

sadlly not working i think the error from where the function is called

It seems like you’re encountering an issue where the cloning and updating of Seciton2 (Section 2) isn’t functioning correctly compared to Seciton1 (Section 1) in your script. Let’s analyze the potential reasons and provide a structured approach to debug and fix the problem.

Analysis and Approach:

  1. Initialization and Cloning Logic:

    • You have two sections (Seciton1 and Seciton2) where you clone a model (FrameModle) and attempt to update it with data from Pages.
  2. Debugging Steps:

    • Check Parental Hierarchy: Ensure that Seciton2 is correctly referenced and that FrameModle is being cloned and attached under Seciton2 when necessary.

    • Conditional Checks (FindFirstChild("Clone")): Verify if the conditional checks (Seciton2:FindFirstChild("Clone")) are correctly identifying the presence of Clone and executing the update logic.

    • Event Handling (NextButton and BackButton): Ensure that when you click NextButton or BackButton, the NextBackFunction correctly updates both sections (Seciton1 and Seciton2) based on the current CurrentPage and MaxPage.

  3. Potential Issues:

    • Parenting of Clones: Double-check if the cloned FrameModle (named Clone) is correctly parented under Seciton2 when key == 2.

    • Update Function (Updateing): Ensure that the Updateing function correctly handles both sections (Seciton1 and Seciton2) and updates the respective Clone based on the provided PageFrame.

  4. Console Output:

    • Utilize print statements effectively to debug. Print out values like Seciton2, Clone, and other key variables to see if they hold expected values during runtime.

Revised Approach:

Based on the analysis, here are the steps to revise and potentially fix the script:

  • Ensure that Seciton2 is correctly referenced and is not nil (print(Seciton2) to verify).
  • Verify that Clone is correctly identified and updated within Seciton2.
  • Check the Updateing function to ensure it properly updates Seciton2.
  • Debug with specific print statements inside the NextBackFunction to see if the control flow reaches the update logic for Seciton2.

Example Adjustments:

local Updateing = function(PageFrame, key, IsItFirstTime)
    Page.Text = "صفحه" .. tostring(MaxPage) .. "/" .. tostring(CurrentPage)
    local Section = key == 1 and Seciton1 or Seciton2
    
    if Section:FindFirstChild("Clone") and not IsItFirstTime then
        local Clone = Section:FindFirstChild("Clone")
        Clone.Position = PageFrame.Position
        Clone.Size = PageFrame.Size
        Clone:WaitForChild("image").Image = PageFrame.ImageId
        Clone:WaitForChild("Name").Text = PageFrame.text
        print("Clone Edited for Section " .. key .. ": ", Clone)
    else
        local Clone = FrameModle:Clone()
        Clone.Name = "Clone"
        Clone.Position = PageFrame.Position
        Clone.Size = PageFrame.Size
        Clone:WaitForChild("image").Image = PageFrame.ImageId
        Clone:WaitForChild("Name").Text = PageFrame.text
        print("Creating new Clone for Section " .. key)
        
        if key == 1 then
            Clone.Parent = Seciton1
        else
            Clone.Parent = Seciton2
        end
    end
end

Conclusion:

Ensure all parts of the script are correctly referenced and updated according to the logic flow you intend. Utilize print statements to debug and verify each step of the process, particularly focusing on the parenting and updating of Seciton2 and its Clone object. This approach should help identify and resolve the issue with Seciton2 not updating as expected.

1 Like

so i should print something for all part in the code so i could debug … thanks i will try to find the bug

1 Like

i think i might know the bug now ```lua

if Seciton1:FindFirstChild(“Clone”) and not IsItFirstTime then

	local Clone = Seciton1:FindFirstChild("Clone")
	Clone.Position =  PageFrame.Position
	Clone.Size = PageFrame.Size
	Clone:WaitForChild("image").Image = PageFrame.ImageId
	Clone:WaitForChild("Name").Text = PageFrame.text
	print("Clone1 Edited, ",Clone)

	return
elseif Seciton2:FindFirstChild("Clone") and not IsItFirstTime  then
	local Clone = Seciton2:FindFirstChild("Clone")
	print(Clone,"Edited?2")

	Clone.Position =  PageFrame.Position
	Clone.Size = PageFrame.Size
	Clone:WaitForChild("image").Image = PageFrame.ImageId
	Clone:WaitForChild("Name").Text = PageFrame.text
	print("Clone2 Edited, ",Clone)
	return
end

Seciton1 will always have a child that called clone so it will always update the clone that in Seciton1 and dont update the clone in Seciton2 cuz i put return so to fix this i think i will add arg that called Seciton1 and it will be true or false if it true it will update Seciton1 if it not true it will update Seciton2 i will see if that will work

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.