Strange error in game

I’m creating a game in it when one part touches another it changes the parent and that part does the same to others. but I have been having some issues with it I have been getting this error The Parent property of Part is locked, current parent: NULL, new parent Junk - Server - JunkConfig:11

Heres the script `function touched(hit)
if hit.Parent:FindFirstChild(“Humanoid”) then
hit.Parent:FindFirstChild(“Humanoid”).Health = 0
elseif hit.Name == “Baseplate” then

else
	hit.Material = Enum.Material.CorrodedMetal
	hit.Anchored = false
	wait(1)
	if hit.Parent == game.Workspace or hit.Parent == game.Workspace:FindFirstChild("Map1") or hit.Parent == game.Workspace:FindFirstChild("Map2") or hit.Parent == game.Workspace:FindFirstChild("Map3") or hit.Parent == game.Workspace:FindFirstChild("Map4") then
	hit.Parent = game.Workspace:WaitForChild("Junk")
	end
end
end

local PartFolder = game.Workspace:WaitForChild(“Junk”)

–add parts with similar properties in the same folder
–or use CollectionService to organize them together
–looping through the entire game isn’t efficient!
local parts = PartFolder:GetChildren()

PartFolder.ChildAdded:Connect(function(child)
if not child then
print(“Bruh”)
else

	child.Touched:Connect(touched)
end

end)`

2 Likes

I tested everything and it works just fine to me

I dont know what could be happening with your game, are any of the parts locked?

2 Likes

Right I think some could be possibly. I’ll check that tomorrow. But even after all the parts that would have a touched event fire for them are destroyed it starts outputting the error is there any way there could be latency or something in the script?

1 Like
function touched(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		hit.Parent:FindFirstChild("Humanoid").Health = 0
	elseif hit.Name == "Baseplate" then

	else
		hit.Material = Enum.Material.CorrodedMetal
		hit.Anchored = false
		task.wait(1)
		if string.match(hit.Parent.Name, "^Map[0-9]+$") then
			hit.Parent = workspace:WaitForChild("Junk")
		end
	end
end

local PartFolder = game.Workspace:WaitForChild("Junk")
local parts = PartFolder:GetChildren()

PartFolder.ChildAdded:Connect(function(child)
	child.Touched:Connect(touched)
end)
1 Like

Thanks forummer! Just a thing to note with sometimes it comes up with this error and temporarily freezes the game in studio but It works fine.

error
ServerScriptService.JunkConfig:10: attempt to index nil with 'Name'
JunkConfig is the name of the script.

function touched(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		hit.Parent:FindFirstChild("Humanoid").Health = 0
	elseif hit.Name == "Baseplate" then

	else
		hit.Material = Enum.Material.CorrodedMetal
		hit.Anchored = false
		task.wait(1)
		if hit.Parent then
			if string.match(hit.Parent.Name, "^Map[0-9]+$") then
				hit.Parent = workspace:WaitForChild("Junk")
			end
		end
	end
end

local PartFolder = game.Workspace:WaitForChild("Junk")
local parts = PartFolder:GetChildren()

PartFolder.ChildAdded:Connect(function(child)
	child.Touched:Connect(touched)
end)
1 Like