-- Handle event fire

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)
