Attachment becoming nil

I need some help debugging. So, I have a coroutine that has a loop inside of it and in the loop there is a variable for an attachment. This attachment becomes “nil” in the script after the variable has been run for the second time and this I demostrated myself by printing the variable where the end result was that at first its name was given and shortly after just nil. The attachment was never removed and there is no script making it nil, so how could it become nil

show code

local att= Stand.PrimaryPart:FindFirstChild("att")
print(att)

22:12:38.317 attachment
22:12:48.384 nil

 local  buildingstuff= coroutine.wrap(function()
				while task.wait(1) do
for _, i in pairs(profile.Data.data) do
					for _, buildings in pairs(build:GetDescendants()) do
						if buildings then
							if buildings.Name == i then
								
				local att= buildings.PrimaryPart:FindFirstChild("att")
												print(att)

The variables not shown are just player data and dont affect the attachment

There’s lots of reasons why it can print as nil. Doesn’t mean it is nil. I have no idea, but you also didn’t give us the code (you took some bits and pieces and stitched those together). Wondering how you get 2x print outputs from 1 print command though

1 Like

Well that I have been wondering too, I thought it would be the loops.

I probably can’t provide the cause of the problem, but I can assure that this is the only code I could find that mentions the attachment or does something to it .

If it really helps I can provide the “full code”, I do still think that its the same , but what can I say I am the one asking for help.

Event.Event:Connect(function(plr)
	if plr then
	local Plot = Plots:FindFirstChild(plr:FindFirstChild("Plot").Value)
	local PlotBuild = Plot:FindFirstChild("Build")
	local profile = datastore.Profiles[plr]
	if profile.Data.Plot then
	
	

		
	
		 local CustomerService = coroutine.wrap(function()
				while task.wait(1) do
				
			for _, i in pairs(profile.Data.Plot) do
					for _, buildings in pairs(PlotBuild:GetDescendants()) do
						if buildings then
							if buildings.Name == i then
								if itemTypes[i] == "Stands" then
										local Stand = buildings
										if Stand and Stand.PrimaryPart then
											
											
										
									local CustomerSpawn = Plot:FindFirstChild("CustomerSpawn")
											if CustomerSpawn:FindFirstChild("Occupied").Value == false then
---------------where the problemo
		local att= Stand.PrimaryPart:FindFirstChild("att")
		print(att)
-----------------------------------									
												local hasCustomer = att:FindFirstChild("HasCustomer")
						if hasCustomer.Value == false then
							hasCustomer.Value = true
							local Customer = Npcs.customer:GetChildren()[math.random(1,#Npcs.customer:GetChildren())]:Clone()
							Customer.Parent = Stand
										
									if Customer and Stand then
--going to add something later 
											
											end
											end
											end
											end
						end
						end
					end
				end	
				
		end
		
		
	end	
			end)
	
	CustomerService()

Please format your code properly. This is a lot of work to read. People want to help you but if you don’t properly indent the code, they’ll click away.

The reason that I want the code is so that I can run it on my machine and debug it for you

1 Like

I mean that was the original code and it is not very organized. If you were to debug it on your end you would need the whole game/project whatever its called. I posted this script because that is where the problem is and was wondering if someone could spot the flaw or maybe help me debug if they happen to know why the variable turns nil when there is nothing changing the variable.

not to sound irritating , but that is the only script I can provide and would like to hear other people’s thoughts on the code being either correct or incorrect which would help me get past that point and know that isn’t the problem’s cause. :grinning:

I came across something interesting while testing and maybe this could help me in the future , but when I started using waitforchild() , an error saying that an infinite yield is happening and that its not looking for the attachment where it should like in the script , but instead has changed and now instead of looking from

its looking from Stand.PrimaryPart.att.children.HumanoidRootPart:WaitforChild("att") so something is very wrong and I don’t know what causes it since my provided code is the only thing that affects the attachment (this change happened after it had already ran once)

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