Part is nil but has no reason to be

Although this sounds vague,
I have a part that I want the player to touch but Roblox says it’s nil but it’s clearly in the workspace.


here is a demonstration:

basically the part is nil in the script even though I’m using the same method as the v:FindFirstChild('Hitbox') and I’ve tried it with other parts within the door and same thing, it says it’s nil(although it didn’t show in the output in the video, it does say it’s nil)


and here is my code for the part and an image of the workspace:

image


local controls = require(plr.PlayerScripts:WaitForChild('PlayerModule'):WaitForChild('ControlModule'))
local effects = plrg:WaitForChild('effects')

local iris = effects.iris.iris
local irisTrigger = iris.Trigger

local misc = RES.misc

local classic_sky = misc:WaitForChild('classic_sky')
local interior_sky = misc:WaitForChild('interior_sky')

local doors = workspace.interact.door

local doorDebounce = false
local interiorDebounce = false
local isInterior = false

local function MoveToPart(destination: BasePart)
	hum:MoveTo(destination.Position)
	hum.MoveToFinished:Wait()
end

for _, v in pairs(doors:GetChildren()) do
	if v:IsA('Model') and v:FindFirstChild('Hitbox') and v:FindFirstChild('back') then
		local hitbox: BasePart = v:FindFirstChild('Hitbox')
		local hitboxBack: BasePart = v:FindFirstChild('back'):FindFirstChild('HitboxBack')
		
		hitbox.Touched:Connect(function(hit)
			local hum: Humanoid = hit.Parent:FindFirstChild('Humanoid')
			
			if hum and not doorDebounce and not isInterior then
				doorDebounce = true
				isInterior = true
				controls:Disable()
				
				v:FindFirstChild('Main').CanCollide = false
				MoveToPart(v:WaitForChild('MoveTo'))
				
				local sky = interior_sky:Clone()
				sky.Parent = game:GetService('Lighting')

				game:GetService('Lighting'):WaitForChild('classic_sky').Parent = game:GetService('Lighting').level
				
				hrp.CFrame = v:FindFirstChild('Location').CFrame
				irisTrigger:Fire(0.25)
				
				task.wait(1)
				
				controls:Enable()
				
				task.wait(5)
				doorDebounce = false
			end
		end)
		
		hitboxBack.Touched:Connect(function(hit)
			local hum: Humanoid = hit.Parent:FindFirstChild('Humanoid')

			if hum and isInterior then
				isInterior = false
				controls:Disable()

				v:FindFirstChild('Main').CanCollide = false

				local sky = classic_sky:Clone()
				sky.Parent = game:GetService('Lighting')

				game:GetService('Lighting'):WaitForChild('interior_sky').Parent = game:GetService('Lighting').level

				hrp.CFrame = v:FindFirstChild('back'):FindFirstChild('Back').CFrame
				irisTrigger:Fire(0.25)

				task.wait(1)

				controls:Enable()
				
				isInterior = false
			end
		end)
	end
end
local hitboxBack: BasePart = v:FindFirstChild('back'):FindFirstChild('HitboxBack')

I don’t see HitboxBack under the back folder. Could that be why you’re receiving a nil error?

oh! I just added it back because I was doing testing seeing if It’d work with the other parts in the back folder and it had the same result. I added it back and same error.

image

1 Like

Is this script running on the launch of the game? If so, the only thing I can think of would be that the part itself hasn’t replicated to the client by the time the script is trying to access it.

Does this issue still happen if you wait for the child instead?

local hitboxBack: BasePart = v:FindFirstChild('back'):WaitForChild('HitboxBack')

Since that seems to be a Local Script, I’d tell you to test out changing the FindFirstChilds from the hitbox and hitboxBack variables with WaitForChilds. I suspect that it’s because it’s not loading as it should, so maybe we can test that out. Depending if that works or not, we may be able to find the issue easier.

it seems to have stopped working completely

1 Like

I suspect that one of the models inside the “door” folder does not have a Hitbox, back or HitboxBack parts. Try checking and noticing if they all have all three of them.

I ended up making a mistake as I set :WaitForChild() for all the :FindFirstChild()s but I only replaced the variables with :WaitForChild() and then it worked!

thank you!

1 Like

Ohh I see… Yeah, me and Uhsleep91 meant only the variables LOL, but glad that could help! Have a good night!

1 Like

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