Attribute is nil at game start

As the tittle says

it worked just fine like yesterday

code:

local stock = game.Workspace.Rollingstock --folder
local loco = stock.Locomotives --yet another folder
local wagon = stock.Wagons -- yet yet another folder
local state = {[true] = 1, [false] = 0}

function onTouched(part:BasePart, hit:BasePart, spring:SpringConstraint, spring_attachment:Attachment, rod:RodConstraint, rod_attachment:Attachment)
	if hit.Name ~= 'Connect' or spring.Attachment1 ~= spring_attachment then
		return
	end
	print(hit:GetAttribute("state"))
	if hit:GetAttribute("state") == true then
		rod.Length = part.Size.Z + hit.Size.Z + .15
		rod.Attachment1 = hit.B

		spring.FreeLength = .1
		spring.Attachment1 = hit.A
	else
		spring.Attachment1 = spring_attachment
		rod.Attachment1 = rod_attachment
	end
end

function OnClick(part:BasePart, spring:SpringConstraint, spring_attachment:Attachment, rod:RodConstraint, rod_attachment:Attachment, chain:BasePart)
	
	spring.Attachment1 = spring_attachment
	rod.Attachment1 = rod_attachment
	
	part:SetAttribute("state", not part:GetAttribute("state"))
	chain.Transparency = state[part:GetAttribute("state")]
	print(part.Parent.Parent, part:GetAttribute("state"))
end

function Search(rolling:Model)
	for i, part:BasePart in ipairs(rolling:GetDescendants()) do
		if part.Name == "Connect" and part:IsA("BasePart") then
			local click:ClickDetector = part.ClickDetector
			local spring:SpringConstraint = part.Spring
			local rod:RodConstraint = part.Rod
			local chain:BasePart = part.Parent.chain
			local rod_attachment:Attachment = part.B
			local spring_attachment:Attachment = part.A

			part.Touched:Connect(function(hit)
				onTouched(part, hit, spring, spring_attachment, rod, rod_attachment)
			end)

			click.MouseClick:Connect(function()
				OnClick(part, spring, spring_attachment, rod, rod_attachment, chain)
			end)
		end
	end
end

function SearchWagon()
	for h, rolling in ipairs(wagon:GetChildren()) do
		Search(rolling)
	end
end

function SearchLoco()
	for h, rolling in ipairs(loco:GetChildren()) do
		Search(rolling)
	end
end

SearchWagon()
SearchLoco()

wagon.ChildAdded:Connect(SearchWagon)
loco.ChildAdded:Connect(SearchLoco)

video:

(this also happens when new wagons/locomotives are spawned in)

any help is greatly appreciated!

NVM there was a typo that did this, whoops

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