How do i fix this script in a local script

Um if you guys can help me out with the Script ill be willing to use it :smile:

Hey there, just stumbled upon this conversation about your scripting issue, i don’t have much time so can you explain what you are trying to do and that it doesn’t work?

1 Like

Your problem is, you’re defining the chapters way before they’re added. With WaitForChild(), will give an infinite yield possible, and FindFirstChild() will return nil. The solution is, to use :FindFirstChild() in those if statements to check if it’s under workspace or not. I also rewrote those variables and added new ones in.

local replicatedStorage = game:GetService("ReplicatedStorage")
local workSpace = game:GetService("Workspace")
local chapters = replicatedStorage:WaitForChild("Chapters")

local gui = script.Parent
local mainFrame1 = gui.Parent
local mainFrame = mainFrame1.Parent
local playFrame = mainFrame:WaitForChild("playFrame")
local resetButton = gui:WaitForChild("ResetBtn")

local Value = game.Workspace.Value -- i have no idea what you use this for, i'll keep it untouched

resetButton.MouseButton1Click:Connect(function()
	
	if workSpace:FindFirstChild("Chapter1") then -- if "Chapter1" exists under workspace
		
		workSpace.Chapter1.Parent = chapters
		mainFrame1.Visible = false
		playFrame.Visible = true

	elseif workSpace:FindFirstChild("Chapter2") then -- if "Chapter1" doesn't exist, check for "Chapter2"
		
		workSpace.Chapter2.Parent = chapters
		mainFrame1.Visible = false
		playFrame.Visible = true

	elseif workSpace:FindFirstChild("Chapter3") then -- you got the hang of it, right?
		
		workSpace.Chapter3.Parent = chapters
		mainFrame1.Visible = false
		playFrame.Visible = true

	elseif workSpace:FindFirstChild("Chapter4") then
		
		workSpace.Chapter4.Parent = chapters
		mainFrame1.Visible = false
		playFrame.Visible = true

	elseif workSpace:FindFirstChild("Chapter5") then
		
		workSpace.Chapter5.Parent = chapters
		mainFrame1.Visible = false
		playFrame.Visible = true
		
	end
	
end)

You could use tables if you know how to, it would shorten your code by 20 lines.
Hope this was useful!

1 Like

Ooh sorry, I didn’t respond to you earlier and thanks it did work!! :smile:
Thank you so much!
I don’t know what went wrong

1 Like

What went wrong is, when Lua was reading your script, it was like:

  • Hey, he put a FindFirstChild() there, let’s see if the object he specified exists
  • Hmm, looks like it doesn’t. I’m gonna make that variable nil
    Then in the statements, it will say:
  • Hey, he wrote an if statement here, let’s see the conditions for it to run.
  • Hmm, those variables that I checked at the start of the game are all nil. I should not run any of these

When checking in the if statements, none would run as all the Chapters were only checked once, at the start of the game. By putting the FindFirstChild()s in the if statements, it will check on the moment if it exists.

  • Hey, he wrote an if statement here, let’s see the conditions for it to run.
  • Hmm, he is checking if Chapter exists under workspace, and it does. So I should run this block of code.

I hope this cleared any questions.
Also, accept the answer as a solution for other people reading the post to know it has been answered

1 Like

Oh now I get I should have put FindFirstChild in the If and elseif statements thanks :smile:

1 Like

i hope i never make that mistake again lol

1 Like