Attempt to index nil with 'WaitForChild'

I am making a computer with an ownership system, and everything should work but it does not.
For instance: for some reason the audios are nil (they werent before). I don’t get why.

local bootButton = script.Parent
local pcScreen = bootButton.Parent
local bootText = pcScreen:WaitForChild("BootText")
local osName = pcScreen:WaitForChild("OSName")
local loadingBarContainer = pcScreen:WaitForChild("LoadingBarContainer")
local loadingBar = pcScreen:WaitForChild("LoadingBar")
local screen = bootButton.Parent.Adornee
local audio = screen:WaitForChild("Sounds"):WaitForChild("Boot"):GetChildren()
local newGUI = bootButton.Parent.Parent.OSScreen -- Set this to your new GUI object (e.g., `newGUI = game.Workspace.NewGUI`)
local remoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("PCBoot")

-- Initial setup: make everything except the button invisible
bootText.Visible = false
osName.Visible = false
loadingBarContainer.Visible = false
loadingBar.Visible = false

-- Utility function to fade in or fade out a GUI element
local function fadeInText(element, duration)
	for i = 0, 1, 0.05 do
		element.TextTransparency = 1 - i
		wait(duration / 20)
	end
	element.TextTransparency = 0
end

local function fadeOutText(element, duration)
	for i = 0, 1, 0.05 do
		element.TextTransparency = i
		wait(duration / 20)
	end
	element.TextTransparency = 1
end

local function fadeInUI(element, duration)
	for i = 0, 1, 0.05 do
		element.BackgroundTransparency = 1 - i
		wait(duration / 20)
	end
	element.BackgroundTransparency = 0
end

local function fadeOutUI(element, duration)
	for i = 0, 1, 0.05 do
		element.BackgroundTransparency = i
		wait(duration / 20)
	end
	element.BackgroundTransparency = 1
end

local function fadeStroke(element, duration, InOut)
	if InOut == "In" then
		for i = 0, 1, 0.05 do
			element.Transparency = 1 - i
			wait(duration / 20)
		end
		element.Transparency = 0
	elseif InOut == "Out" then
		for i = 0, 1, 0.05 do
			element.Transparency = i
			wait(duration / 20)
		end
		element.Transparency = 1
	end
end

local function simulateLoadingBar()
	local startXSize = -0.592  -- Start from 0 size
	local endXSize = 0.175  -- End size
	local totalSteps = 40  -- Number of steps for the loading bar
	local stepTime = 0.1   -- Time between each step

	-- Start the loading bar at the initial size
	loadingBar.Size = UDim2.new(startXSize, loadingBar.Size.X.Offset, loadingBar.Size.Y.Scale, loadingBar.Size.Y.Offset)

	for i = 0, totalSteps do
		-- Calculate the current size based on the progress
		local currentXSize = startXSize + ((endXSize - startXSize) * (i / totalSteps))
		loadingBar.Size = UDim2.new(currentXSize, loadingBar.Size.X.Offset, loadingBar.Size.Y.Scale, loadingBar.Size.Y.Offset)

		wait(stepTime)

		-- Randomly pause to mimic real loading
		if math.random() < 0.2 then
			wait(math.random(1, 3)) -- Simulate a freeze
		end
	end
end


local debounce = false

-- Click event for the boot button
bootButton.MouseButton1Click:Connect(function()
	if debounce then
		return
	end
	audio[math.random(1,#audio)]:Play()
	
	debounce = true
	remoteEvent:FireServer()
	bootButton.Active = false
	bootButton.Interactable = false
	-- Fade out the boot button
	coroutine.wrap(function()
		fadeOutUI(bootButton, 1)
	end)()
	fadeOutText(bootButton, 1)

	-- Fade in booting text, OS name, and loading bar
	wait(0.5) -- Small delay for effect
	bootText.Visible = true
	osName.Visible = true
	loadingBarContainer.Visible = true
	loadingBar.Visible = true
	fadeInText(bootText, 1)
	fadeInText(osName, 1)
	fadeStroke(loadingBarContainer.UIStroke, 1, "In")
	fadeInUI(loadingBar, 1)

	-- Start the loading bar simulation
	wait(0.5) -- Small delay for effect
	simulateLoadingBar()

	-- Once loading is complete, hide everything and enable the new GUI
	wait(1) -- Short delay before switching
	bootText.Visible = false
	osName.Visible = false
	loadingBarContainer.UIStroke.Transparency = 1
	loadingBar.Visible = false

	-- Enable the new GUI here
	if newGUI then
		pcScreen.Enabled = false
		newGUI.Enabled = true
	end
	debounce = false
end)

The hierarchy:
image

The hierarchy of adornee:
image

2 Likes

can you show the error in the output?

StarterGui.ComputerScreens.Lab1.BootScreen.Boot.Script:8: attempt to index nil with 'WaitForChild' - Client - Script:8

The Adornee most likely hasn’t loaded in yet, maybe try waiting for the adornee and add it as an adornee in a local script

1 Like

Yeah but the thing is, despite everything the script works and the sounds play.

this means that you are trying to look for an object inside nothing !
by other meaning you are trying to call waitforchild on a nil

Adornee is not an instance, it’s a property, so it wouldn’t have a waitforchild method. If adornee is an instance (which is what I suspect), there is no “adornee” inside the BootScreen ScreenGUI. Did you perhaps reference the adornee wrongly?

1 Like

Sorry, I was wrong on my first reply.
screen is bootButton.Parent.Adornee , which bootButton is script.Parent .
You’re trying to get the adornee of this button.
image

wrong. hes trying to get Adornee property of “ScreenGui”, Adornee is a property of BillboardGui not ScreenGui

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