local function X(X,G)local E if G=="\102\114\101\101\122\101\114"then E=freezerMotor elseif G=="\100\111\111\114"then E=doorMotor else return end if X.Character and X.Character:FindFirstChild("\084\111\114\115\111")then local m=X.Character:FindFirstChild("\084\111\114\115\111")local C=m.Position-fridge.FreezerPadding.Position if C.magnitude<clickDistance then if open then E.DesiredAngle=0 open=false else E.DesiredAngle=math.pi/2 open=true end if G=="\102\114\101\101\122\101\114"then fridge.FreezerBlock.Smoke.Enabled=open end end end end fridge.DoorPadding.ROBLOXInteractionEvent.OnServerEvent:connect(function(G,E)X(G,"\100\111\111\114")end)fridge.Door.ROBLOXInteractionEvent.OnServerEvent:connect(function(G,E)X(G,"\100\111\111\114")end)fridge.FreezerPadding.ROBLOXInteractionEvent.OnServerEvent:connect(function(G,E)X(G,"\102\114\101\101\122\101\114")end)fridge.FreezerDoor.ROBLOXInteractionEvent.OnServerEvent:connect(function(G,E)X(G,"\102\114\101\101\122\101\114")end)

local function toggleDoor(player, door)
	local motor
	if door == "freezer" then
		motor = freezerMotor
	elseif door == "door" then
		motor = doorMotor
	else
		return
	end
	
	
	-- Check distance to the character's Torso. If too far, don't do anything
	if player.Character and player.Character:FindFirstChild("Torso") then	
		local torso = player.Character:FindFirstChild("Torso")
		local toTorso = torso.Position - fridge.FreezerPadding.Position
		if toTorso.magnitude < clickDistance then
			if open then
				motor.DesiredAngle = 0
				open = false
			else
				motor.DesiredAngle = math.pi/2
				open = true
			end
			if door == "freezer" then
				fridge.FreezerBlock.Smoke.Enabled = open
			end
		end
	end
end

fridge.DoorPadding.ROBLOXInteractionEvent.OnServerEvent:connect(function(player, arguments)
	toggleDoor(player, "door")
end)

fridge.Door.ROBLOXInteractionEvent.OnServerEvent:connect(function(player, arguments)
	toggleDoor(player, "door")
end)

fridge.FreezerPadding.ROBLOXInteractionEvent.OnServerEvent:connect(function(player, arguments)
	toggleDoor(player, "freezer")
end)

fridge.FreezerDoor.ROBLOXInteractionEvent.OnServerEvent:connect(function(player, arguments)
	toggleDoor(player, "freezer")
end)
