Pathing part within a Instanced model, returning nill?

Alright so heres my spaghetti code in all its glory so you can see:

local part = script.Parent.p1
local Players = game:GetService("Players")
local Working = false
local HasPlayerblock = false
local CurrentPlayerBlock
local CurrentPlayerBlockCFrame
local EmptyVector = Vector3.new()
local iftrue = false

part.Touched:Connect(function(hit2)
	if Working == false then
	print("function 1 started check")
	local PlayerBlock = hit2
	print (PlayerBlock)
	local CurrentPlayerTouched = game.Players:FindFirstChild("Humanoid")
	local parthit = string.find(PlayerBlock.name,"Player/", 1)
	if parthit == 1 and Working==false then
		print("found PlayerBLock")
		HasPlayerblock = true
		CurrentPlayerBlock = PlayerBlock
		CurrentPlayerBlockCFrame = CurrentPlayerBlock.CFrame
		print(CurrentPlayerBlockCFrame)
		print("Recorded CFrame of PlayerBlock")
		end	
		end
end)

part.Touched:Connect(function(hit)
	local Humanoid = hit.Parent:FindFirstChild("Humanoid")
	local CurrentPlayerTouched = game.Players:FindFirstChild(hit.Parent.Name)
	local p1Vector = Vector3.new()
	local p2Vector = Vector3.new()
	p1Vector = script.Parent.p1.CFrame
	p2Vector = script.Parent.p2.CFrame
	print("Touched, CFrame recorded...")
	if Humanoid and Working==false then
		Working = true
		Humanoid.WalkSpeed = 0
		local HS = Humanoid.HeadScale
		local BDS = Humanoid.BodyDepthScale
		local BWS = Humanoid.BodyWidthScale
		local BHS = Humanoid.BodyHeightScale
		local rk = org:Clone()
		rk.Parent = script.Parent
		rk:MakeJoints()
		wait(0.05)
		rk:SetPrimaryPartCFrame(p1Vector:Lerp(p2Vector,0.5))
		wait(0.0005)
		print("Cloned House2 and CFrame'd")
		print("Working on player now...")
		CurrentPlayerBlock.CFrame = CurrentPlayerBlockCFrame + Vector3.new(0,50,0)
		wait(0.0005)
		HS.Value = .75
		BDS.Value = .75
		BWS.Value = .75
		BHS.Value = .75
		wait(0.0005)
		CurrentPlayerBlock.CFrame = p1Vector:Lerp(p2Vector,0.48)
		wait(0.0005)
		CurrentPlayerTouched.CameraMinZoomDistance = 15
		wait(0.0005)
		CurrentPlayerTouched.CameraMaxZoomDistance = 15
		wait(0.0005)
		print(CurrentPlayerTouched.CameraMaxZoomDistance)
		--rk.Parent = script.Parent
		Humanoid.WalkSpeed = 8
		print("All done, moving to watch loop")
		iftrue = true
		local door = rk:FindFirstChild("door") --also have tried WaitForChild("door")
		while iftrue == true do
			wait(0.0005)
			door.Touched:Connect(function(hit3)
				print("player touched return door")
				local lerpReversed = EmptyVector - p1Vector:Lerp(p2Vector,0.48)
				HS.Value = .15
				BDS.Value = .15
				BWS.Value = .15
				BHS.Value = .15
				CurrentPlayerBlock.CFrame = lerpReversed
				print("while true done, CFramed player")
				iftrue = false
			end)
		end
	end
	
end)

the problem line is:
local door = rk:FindFirstChild("door")

heres what im trying to achieve:

I find the block attached to the player, save the cframe
create clone rk(house2)
set cframe of clone
set cframe of block attached to player
listen for player to touch “door” part from clone
return player back with cframe
then cleanup the model clone(not added yet)

im getting a nill value as I cant figure out how to path to the part “door” within the model “house2” that has a set parent of Script.Parent

Any help would be appreciated!

2 Likes

The first error is occurring because the correct method is :FindFirstChild() – as far as the infinite yield goes, however, that means that the script was unable to find an Instance called “door” inside of “rk”.

Remember that this will be case specific, so if the Instance has any capital letters, you need to account for that. If that doesn’t end up resolving it, it’s likely that the door you’re trying to reference is in another location. I would advise looking through the hierarchy of “rk” before playtesting & then while playtesting to see where the “door” is in relation to that Instance.

2 Likes

all mentions of rk are lowercase, and in replicated storage its ReplicatedStorage>house2>door

door is the primary part of house2 which im basing the position of the cloned model CFrame off of. Im just having trouble calling it specificly when its inside a cloned model



^Current hierarchy order

when the script is running the cloned house2(rk) has its parent set to house 3(script.Parent)

your right about it returning a nill value now that I have the correct :FindFirstChild()
but im unsure as to why still

also note the script inside house2 is empty, I just forgot to clean that up. this is all running from a script located in house 3

1 Like

Updated the thread to better reflect my issue better/ fix a syntax error

1 Like

Main Suggestion

Before looking for the door inside of “rk”, try printing rk:GetChildren() to see a table in the Output that will display all of that Instance’s Children – this is where we can see if the door is contained within that layer prior to the check being made.


Secondary Suggestions

Considering some of the code is being ran in a loop (more specifically, where the Touched event of the door is being connected to a function), you should check if the door exists before doing that.

For example:

local door = rk:FindFirstChild("door")

if door then
    door.Touched:Connect(function(hit3)
        -- Continue
    end)
end



Furthermore, while I don’t know what the specific use case is for this, it doesn’t seem necessary to wrap the function in a loop like this since the code inside of the function will only be activated if the player touches the door.

Also, I’d like to point out that using wait(n) where n is <= 0.03 won’t be any different than just using wait(), so those could be removed/transitioned to just wait().

1 Like

the output of GetChildren was

 [1] = stairs,
 [2] = door , <<<< I think I might be dumb hold up
 [3] = walls,
 [4] = Script

so should I be searching for “door,” or is that just how the GetChildren outputs?

Im trying to do the touched event within another touched event, so I figured by putting it in a while loop I could have the script “listen” for door to be touched before ending the first touched function.

Not much difference than putting the while loop after the touched event, but both throw this nill value for part

I think I read that somewhere but I didnt know wait() by default did the minium, I figured it’d be 1 second good to know thanks.

so Ingame, the part IS named door, but the output on children is door with a space for some reason?

1 Like

If you got that directly from the Output this might mean that the name of the Instance is "door " with an extra space at the end, which is why the script wasn’t able to find it when referencing “door” without a space.

Before playtesting, try editing its name to see if there’s an extra space.

1 Like

Okay, so that definately WAS the issue, but the part is NOT named with a extra space after I checked editing the name. Weird but thanks for helping me learn how to identify these issues in the future!

also you were right about the while loop being pointless, you can put a touched:connect within a touched:connect if you want too

2 Likes